Jump to content

[SOLVED] My upload script not working??? Help Please...


StefanRSA

Recommended Posts

I think I miss something small....

Can anybody tell me where is the problem??? My script does not upload any images...???

The image folder is 777,

The

ERROR_REPORTING(E_ALL); 

reveal no error....?

And the

print_r($_FILES);

Give me the following:

Array ( [newImage] => Array ( [name] => Array ( [0] => Photo0088.jpg [1] => [2] => [3] => [4] => [5] => [6] => ) [type] => Array ( [0] => image/jpeg [1] => [2] => [3] => [4] => [5] => [6] => ) [tmp_name] => Array ( [0] => /tmp/phpIJa7Cz [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] => 108092 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 ) ) ) 

 

Can anybody please have a look at the code and tell me if I am just being stupid and don't see the error????

 

Thanks!

 

The Script:

<?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);

?>

 

I fixed this problem thanks to someone in another forum (DevNetwork)

My problem was as follow:

 

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

 

To the submit button input, add

name="btnSubmit"

 

Then change the condition on line 11 to

   1. if(isset($_POST['btnSubmit']))

That's the initial hurdle.

 

The complete working code:

<?php
ERROR_REPORTING(E_ALL); 
//IMPORTANT VARIABLES!!!
$widthMain=100;
$heightMain=100;
$widthThumb=50;
$heightThumb=50;
$imgDir='';//remember trailing '/' if not blank

if(isset($_POST['btnSubmit'])){
//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" name="btnSubmit"></form>';
print_r($_FILES);
ini_set("display_errors", true);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.