Upload image file to two directories php

Ask about a PHP problem here.
Post Reply
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Upload image file to two directories php

Post by killfrog47 »

So I have been looking at this problem for the past 3 days and I haven't gotten much closer to a solution. Here is what I am trying to accomplish in a flowchart format. Now I have been playing with a lot of different code and reading different things so my code keeps changing. The code is all garbage. I am just needing to know where I should start? I just need a kick in the right direction. I've looked through php.net and other posts on stackoverflow but no luck =/ Thank you!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Upload image file to two directories php

Post by ExtremeGaming »

I can't see your flowchart very well, but here is a basic explanation of uploading. Feel free to ask any other questions towards the subject.

You're going to need to first work on security. Uploading is a very very dangerous thing if exploited (they can access all of your files). Look into the $_FILES superglobal. You can use this to determine various things such as: File size, File extension, and the name, etc. This is also required for uploading.

The absolute first thing you want to do before working on your actual goal, is check for security. First set a standard on what file extensions you want to be allowed. Use something like:

[syntax=php]<?php

$extensions = array("jpg", "jpeg", "png", "gif");

$extension = end(explode(".", $_FILES['upload']['name']));

if(!in_array($extension, $extensions)) {
die("Extension not allowed");
}
?>[/syntax]
Now, you're of course going to want to check for more things, but you must make sure they can only upload what you allow them to at the very least.

If you want to convert a file to use a specific extension, use imagejpeg() or imagepng() or imagegif()

Once you have finished security, processing, and converting, you next want to upload your image. To do this, use move_uploaded_file(). Note, that if the image already exists, it will be overwritten. That is why you want to rename your images as well. Here is what I would use to rename an image:

[syntax=php]<?php

// Get the extension
$file_ext = end(explode(".", $_FILES['upload']['name']));
// Get the temporary image file, received upon submission
$tmp = $_FILES['upload']['tmp_name'];
// The upload path
$target = "uploads/";

// Here is where you rename the file
$new_file = uniqid() . ".$file_ext";

// Create your new target. uploads/(uniqid output).(file extension)
$target = $target.$new_file;

// Move the uploaded file to the target
move_uploaded_file($tmp, $target);

?>[/syntax]
<?php while(!$succeed = try()); ?>
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Upload image file to two directories php

Post by killfrog47 »

ExtremeGaming wrote:I can't see your flowchart very well, but here is a basic explanation of uploading. Feel free to ask any other questions towards the subject.


Very helpful thank you so much! Now as far as security goes my client is the only one uploading and he is the only one who wants access to the page/control panel so im not TOO worried about that though it is probably a good thing to work on.

Now one thing i need to do is make these photos in to thumbnails and in order to do that i need a reduced quality/size copy of the image because this client uploads very big images and they take forever to load if i was to make a gallery of them. So how would i go about that? That is where i am stumped I dont know how to upload to two places (photos/large and photos/thumb) and idk how to change the size of the images as they go to the "photos/thumb" folder.

Sorry about the flowchart size. I never used that site before lol it was worth a shot =P
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Upload image file to two directories php

Post by ExtremeGaming »

Resizing images is a several step process. I don't have time to test/write out an explanation, so I'll get you started with this. Look at the example and look up the other functions it uses.

imagecopyresampled()
<?php while(!$succeed = try()); ?>
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Upload image file to two directories php

Post by killfrog47 »

ExtremeGaming wrote:Resizing images is a several step process. I don't have time to test/write out an explanation, so I'll get you started with this. Look at the example and look up the other functions it uses.

imagecopyresampled()


Alright ill read up on it. Now one thing is that I am having trouble getting my head around how to use these things. lol Ill come back here if I have any questions. Im gonna see if i can write a code =)
Post Reply