PHP Upload - Not Working, No Error.
Hi,
I have 6 upload fields to add images to a "project."
The HTML for the form is as follows:
HTML Code:
<form method="post" action="cms_artwork.php?artwork_page=add_proj" enctype="multipart/form-data">
<table cellpadding="3px" cellspacing="3px">
<tr>
<td>
<strong>one:</strong> <input type="file" name="img1" /><br />
<em>caption:</em> <input type="text" name="cap1" />
</td>
<td>
<strong>two:</strong> <input type="file" name="img2" /><br />
<em>caption:</em> <input type="text" name="cap2" />
</td>
</tr>
<tr>
<td>
<strong>three:</strong> <input type="file" name="img3" /><br />
<em>caption:</em> <input type="text" name="cap3" />
</td>
<td>
<strong>four:</strong> <input type="file" name="img4" /><br />
<em>caption:</em> <input type="text" name="cap4" />
</td>
</tr>
<tr>
<td>
<strong>five:</strong> <input type="file" name="img5" /><br />
<em>caption:</em> <input type="text" name="cap5" />
</td>
<td>
<strong>six:</strong> <input type="file" name="img6" /><br />
<em>caption:</em> <input type="text" name="cap6" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="projid" value="<?php echo $projid; ?>" />
<input type="submit" value="Add Images" id="config_submit" />
</td>
</tr>
</table>
</form>
The PHP is as follows:
PHP Code:
$projid = $_POST['projid'];
$projTitle = 'Undefined Title';
$result = mysql_query("SELECT * FROM artwork_projs WHERE projid='$projid'");
$count = mysql_num_rows($result);
if($count > 0){
$row1 = mysql_fetch_assoc($result);
$projTitle = $row1['title'];
$result = mysql_query("SELECT img_num FROM artwork_images WHERE projid='$projid' ORDER BY img_num ASC");
$count = mysql_num_rows($result);
if($count > 0){
$row = mysql_fetch_assoc($result);
$lastNum = array_pop($row);
$firstNum = $lastNum + 1;
echo $firstNum;
}
else{
$firstNum = 1;
}
$upload_success = false;
print_r($_FILES);
function addImages($inputName){
global $firstNum, $projid, $upload_success;
if((!empty($_FILES[$inputName])) && ($_FILES[$inputName]['error'] == 0)) {
$filename = basename($_FILES[$inputName]['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == 'jpg') || ($_FILES[$inputName]['type'] == 'image/jpeg')){
$newname = 'imgs/artwork/'.$projid.'_'.$firstNum.'.'.$ext;
if(file_exists($newname)){
unlink($newname);
}
if(move_uploaded_file($_FILES[$inputName]['tmp_name'],$newname)){
$capInputNum = substr($inputName,3);
$imgCaption = isset($_POST['cap'.$capInputNum]) ? trim($_POST['cap'.$capInputNum]) : '<span></span>';
$result = mysql_query("INSERT INTO artwork_images (projid,img_num,main,caption) VALUES ('$projid','$firstNum','no','$imgCaption'");
$firstNum++;
$upload_success = true;
}
else{
echo '<p>'.$inputName.' NOT UPLOADED</p>'."\n";
}
}
else{
echo '<p>'.$inputName.' NOT A JPG</p>'."\n";
}
}
}
for($i=1;$i<=6;$i++){
addImages('img'.$i);
}
if($upload_success == true){
echo '<h2>Images Successfully Added to <em>'.$projTitle.'</em></h2>'."\n";
echo '<p>The images you added to <em>'.$projTitle.'</em> are now part of the project.</p>'."\n";
echo '<p>'.$firstNum.'</p>'."\n";
}
else{
echo '<h2>There was a problem</h2>'."\n";
echo '<p>For some reason, the images for <em>'.$projTitle.'</em> were not added successfully. Please try again.</p>'."\n";
}
}
else{
redirect2('cms_artwork.php?artwork_page=add_proj&projDoesNotExist');
}
When I print_r($_FILES);, I get the following:
Code:
Array (
[img1] => Array (
[name] => artworktestj2.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpuFLMRW
[error] => 0 [size] => 3698 )
[img2] => Array (
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0 )
[img3] => Array (
[name] => [type] =>
[tmp_name] =>
[error] => 4
[size] => 0 )
[img4] => Array (
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0 )
[img5] => Array (
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0 )
[img6] => Array (
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0 ) )
Now, I've made sure the folder has the correct permissions, that PHP's temp directory is set to "/tmp", that the max upload size is 5M, etc. I know it might work better if I set the names of the upload inputs to images[] or something, to force the array, but I don't know how to include the individual captions that way (would I do caption[] or something?). If anyone has any suggestions or ideas as to why this isn't working, your help is appreciated. Thank you!