Upload script not working... Please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
StefanRSA
Forum Newbie
Posts: 5
Joined: Thu Jun 25, 2009 12:43 pm

Upload script not working... Please help

Post by StefanRSA »

:banghead: Hi, I have the following code but its not working... My upload folder permission is fine, and even with errror check don't I get any error...? Please look at my code and help me please...?

Thank You...

Code: Select all

<?php
ERROR_REPORTING(E_ALL); 
//IMPORTANT VARIABLES!!!
$widthMain=100;
$heightMain=100;
$widthThumb=50;
$heightThumb=50;
$imgDir='../ad_images/';//remember trailing '/' if not blank
 
if($_POST){
//form was posted
//do other stuff here
 
//create record and get ad_id
$thisAdId='1234';
 
if($_FILES){//for uploaded files
//image names in form are newImage[]
foreach($_FILES['newImage']['tmp_name'] AS $imId=>$uploadedImage){
 
if($_FILES['newImage']['size'][$imId]!=''){//make sure that an image was uploaded
$fileName=$imgDir.$_FILES['newImage']['name'][$imId]; //get original name of uploaded image
move_uploaded_file($uploadedImage,$fileName);//copy upload from tmp dir to current dir so we can work with it
list($width, $height) = getimagesize($fileName); //get image dimensions
$imgPathMain=$imgDir.$thisAdId.'_'.$imId.'.jpg'; //main pic name
$imgPathThumb=$imgDir.$thisAdId.'_'.$imId.'_t.jpg'; //thumbnail name
 
//main image
$imageMain=imagecreatetruecolor($widthMain, $heightMain); //create new image resource
$image=imagecreatefromjpeg($fileName); //get uploaded image data
imagecopyresampled($imageMain, $image, 0, 0, 0, 0, $widthMain, $heightMain, $width, $height);//copy upload to new main pic
imagejpeg($imageMain, $imgPathMain, 100); //create main image
 
//thumbnail
$imageThumb=imagecreatetruecolor($widthThumb, $heightThumb); //create new image resource
imagecopyresampled($imageThumb, $image, 0, 0, 0, 0, $widthThumb, $heightThumb, $width, $height);//copy upload to new thumbnail pic
imagejpeg($imageThumb, $imgPathThumb, 100); //create main image
 
unlink($fileName);//delete original upload because we don't need it any more
}
}
}
}
 
//my example image upload form
echo '<hr><form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">';
for($i=0;$i<=6;$i++){
echo '<input type="file" name="newImage[]"><br>';
}
echo '<input type="submit" value="Upload Images"></form>';
print_r($_FILES);
 
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Upload script not working... Please help

Post by requinix »

So if you don't get any error how do you know it's not working?
StefanRSA
Forum Newbie
Posts: 5
Joined: Thu Jun 25, 2009 12:43 pm

Re: Upload script not working... Please help

Post by StefanRSA »

No images gets uploaded....? No errors display if i use ERROR_REPORTING(E_ALL);
And I get the following in my array if I use print_r($_FILES);
::
Array ( [newImage] => Array ( [name] => Array ( [0] => Photo0058.jpg [1] => [2] => [3] => [4] => [5] => [6] => ) [type] => Array ( [0] => image/jpeg [1] => [2] => [3] => [4] => [5] => [6] => ) [tmp_name] => Array ( [0] => /tmp/phpRt4M1Q [1] => [2] => [3] => [4] => [5] => [6] => ) [error] => Array ( [0] => 0 [1] => 4 [2] => 4 [3] => 4 [4] => 4 [5] => 4 [6] => 4 ) [size] => Array ( [0] => 148897 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) )


And allas... No image uploaded???
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Upload script not working... Please help

Post by requinix »

Have you checked the display_errors setting?

Code: Select all

ini_set("display_errors", true);
StefanRSA
Forum Newbie
Posts: 5
Joined: Thu Jun 25, 2009 12:43 pm

Re: Upload script not working... Please help

Post by StefanRSA »

Not even ini_set("display_errors", true); display any error???
Not even in my server error log is anything...?

Can anybody please test this code for me? I am sure this should work???
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload script not working... Please help

Post by McInfo »

The processing section of your script is not being triggered because there are no inputs in your form that populate $_POST. $_POST is an empty array, so the condition on line 11 is false even after you submit the form.

To your submit button input, add

Code: Select all

name="btnSubmit"
Then change the condition on line 11 to

Code: Select all

if(isset($_POST['btnSubmit']))
That's the initial hurdle. I haven't debugged past this point.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:20 pm, edited 1 time in total.
StefanRSA
Forum Newbie
Posts: 5
Joined: Thu Jun 25, 2009 12:43 pm

Re: Upload script not working... Please help

Post by StefanRSA »

McInfo!!! You are a STAR!!!!! Thanks a mil!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload script not working... Please help

Post by McInfo »

Since you are discarding the original file anyway, you can use the temp file in your $uploadedImage variable instead of making a copy of it with move_uploaded_file(). The tmp_name file is an actual file and can be manipulated just like any other. The only difference is that it gets destroyed by PHP's garbage collection after the script finishes, which is similar to what you are doing with unlink().

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:21 pm, edited 1 time in total.
StefanRSA
Forum Newbie
Posts: 5
Joined: Thu Jun 25, 2009 12:43 pm

Re: Upload script not working... Please help

Post by StefanRSA »

I actually want the bigger image to be a maximum of 600px and this will be uploaded by any registered web user. It might be then that a user might upload massive images. Thats why the reason of creating two images... Or do I misunderstand what you are trying to say McInfo?

Thanks Anyway!!!

Stefan
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Upload script not working... Please help

Post by McInfo »

I'm talking about the temporary file that the PHP server creates when you upload a file. If you are not saving the file in its original form, you can work with the temporary file instead of moving and then deleting it.

You do get an extra validation check by using move_uploaded_file(), but you could get the same benefit with is_uploaded_file().
PHP Manual wrote:move_uploaded_file() -- This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

is_uploaded_file() -- Returns TRUE if the file named by filename was uploaded via HTTP POST. This is useful to help ensure that a malicious user hasn't tried to trick the script into working on files upon which it should not be working--for instance, /etc/passwd.
Edit: This post was recovered from search engine cache.
Post Reply