Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

multiple Image upload fails to upload only the first image

php html mysql mysqli database forms upload

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 WebNinja

WebNinja

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 28 July 2013 - 03:26 AM

I have a form that allows the user to upload multiple images, with each image capped at 2MB. After the form is submitted, the images would be stored on the server(E.g my computer in this case).


***PROBLEM***
When uploading multiple files, the first image that is uploaded will always fail in uploading to the server, while the others(E.g 2nd,3rd, etc) are able to be saved to my computer successfully.When testing the form with just **ONE** image, the image also fails to upload.

I've concluded that the first image that is submitted through the form will always fail to upload though i fail to see why that is the case as i am able to echo the tmp_name of the image files successfully.

**MY CODE(Extract):**


if(isset($_FILES['upload']['tmp_name']))
{
$numfile=count($_FILES['upload']['tmp_name']);


for($i=0;$i<$numfile;$i++)
{
if(!empty($_FILES['upload']['tmp_name'][$i]))
{
if(is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
{


//Conditionals for uploaded file
$foldername=$_SESSION['UserId'];
$cat=$_POST['category'];
$sub=$_POST['subcat'];
$itemname=$_POST['itemname'];
$allowed_filetypes=array('.jpg','.gif','.bmp','.png','.JPG');
$max_filesize = 2097152; // Maximum filesize in BYTES (currently 2.0MB).
$upload_path = 'C:\Users\Kence\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\'; // The place the files will be uploaded to.


//Checks if Folder for User exists
//If not, A folder for the user is created to store the user's images
if(!is_dir($upload_path))
{
$upload_path=mkdir($upload_path,0644,true);
}


$filename = $_FILES['upload']['name'][$i]; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.


// Check if the filetype is allowed, if not DIE and inform the user.
if(in_array($ext,$allowed_filetypes))
{
if(filesize($_FILES['upload']['tmp_name'][$i])<$max_filesize)
{
if(is_writable($upload_path))
{echo"$upload_path <br>";
print_r($_FILES);
if(!move_uploaded_file($_FILES['upload']['tmp_name'][$i],"$upload_path" . $filename))
{
$errormsg="Upload Failed.Error in moving file";
}
}
else
{
$errormsg="Image Upload Failed.";
}
}
else
{
$errormsg="Upload failed.ONly images below 2MB are allowed";
}
}
else
{
$errormsg="Error. Only JPEG,PNG and BMP files are allowed";
}


}
else
{
$errormsg="Error.File not uploaded";
}
}
}


}
else
{
$errormsg="Upload failed";
}


**Error messages**
I get the error message





> Error.File not uploaded


**Form Code:**


<body onload="getcategory()">
<form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="additem" enctype="multipart/form-data" method="POST">
<table>
<tr>
<td>Select Category: </td><td>
<select name="category" id="category" onchange="getsubcategory(this)">
<option disabled="disabled" value="">Choose a category</option>
</select>
</td>
</tr>
<tr>
<td>Select SubCategory</td>
<td>
<select id="subcat" name="subcat">
<option value=""></option>
</select>
</td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" size="30" maxlength="50" required="required"></td>
</tr>
<tr>
<td>Item Price</td>
<td><input type="number" name="itemprice" size="30" min="1" required="required"></td>
</tr>
<tr>
<td>Item Info</td>
<td><textarea name="iteminfo" col="40" rows="10" maxlength="300" required="required"></textarea>
</tr>


<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>


<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>


<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>


<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>


<tr>
<td>Filename:</td>
<td><input type="file" name="upload[]" /></td>
</tr>
<tr>
<td colspan="2"><input type="SUBMIT" name="Button" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><?PHP if(isset($errormsg)){echo"$errormsg";}?></td>
</tr>
<tr>
<td colspan="3"><font color="#FF0000"></font></td>
</tr>
</table>
</form>
</body>

Even though i am able to `print_r ($_FILES)` and the `tmp_name` of all the images im trying to upload will display successfully.

**EDIT**
*Step Taken/Tried*
Added an if(!empty) check so that only uploads with tmp_name are processed.Empty uploads are thus ignored, thereby removing the error

Error.File not uploaded

However, even with that, the first image that is submitted to the form will **NOT** be saved(I.e if i upload just one image, if would fail,without any errors, and if i upload multiple images, only the first image would not be uploaded while the rest would be saved successfully.) However, if i were to refresh the page(thereby resubmitting the same form) the first image would then be saved.

The problem here is that the file path is **NOT** writable, causing the first image to not be saved.However what is confusing me is that, if the file path for the first image is not writable, why would the subsequent images have a valid file path, when they are running on the same code? Also, why would refreshing the page( and thus resubmitting the form) magically let the first image be saved successfully(which eans the file path is now writable?)

**Results of my print_r($_FILES);**


Array (
[upload] => Array (
[name] => Array (
[0] => download (1).jpg
[1] => download.jpg
[2] =>
[3] =>
[4] =>
)
[type] => Array (
[0] => image/jpeg
[1] => image/jpeg
[2] =>
[3] =>
[4] =>
)
[tmp_name] => Array (
[0] => C:\Users\Kence\Desktop\UniServer\tmp\php474.tmp
[1] => C:\Users\Kence\Desktop\UniServer\tmp\php475.tmp
[2] =>
[3] =>
[4] =>
)
[error] => Array (
[0] => 0
[1] => 0
[2] => 4
[3] => 4
[4] => 4
)
[size] => Array (
[0] => 6229
[1] => 6984
[2] => 0
[3] => 0
[4] => 0
)
)
)


Could someone point out where and what im doing wrong? I've been trying for the past 3 hours and i still don't know what i'm doing wrong and it's driving me crazy!

Edited by WebNinja, 28 July 2013 - 07:18 AM.


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 02 August 2013 - 07:38 PM

Shouldn't you then make sure first the path is writable before starting the upload files iteration?

meaning, all about the directory first, then the loop






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download