Jump to content

File upload [PHP] [MYSQL]


imdead

Recommended Posts

Hey guys i have a problem,

I have a form to upload 2 image files  however sometimes i want to only upload the one file, but if i only upload one image, it tells me that it can't upload because there is no name for the second image.

 

I tried fixing it with if (isset($fieldname2))  however i just realised that $feildname2 is always set so im not quite sure how to fix?

 

($action == "add"){
$add = $_POST['add'];
$reference = $_POST['reference'];
$category = $_POST['category'];
$sub_cat = $_POST['sub_cat'];
$fieldname = 'file';
$fieldname2 = 'file2';
error_reporting(E_ALL);
ini_set('display_errors',1);
is_uploaded_file($_FILES[$fieldname]['tmp_name']);
getimagesize($_FILES[$fieldname]['tmp_name']);
if (isset($fieldname2)) {
is_uploaded_file($_FILES[$fieldname2]['tmp_name']);
getimagesize($_FILES[$fieldname2]['tmp_name']);
}
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$_FILES[$fieldname]['name']))
{
    $now++;
}
if (isset($fieldname2)) {
while(file_exists($uploadFilename2 = $uploadsDirectory.$_FILES[$fieldname2]['name']))
{
    $now++;
}
}
move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
if (isset($fieldname2)) {
move_uploaded_file($_FILES[$fieldname2]['tmp_name'], $uploadFilename2);
}
$sqlname = $_FILES[$fieldname]['name'];
if (isset($fieldname2)) {
$sqlname2 = $_FILES[$fieldname2]['name'];
}
$id = mysql_insert_id();
$query = "INSERT INTO cakes (id, reference, category, sub_cat, image, image2) VALUES ('$id', '$reference', '$category', '$sub_cat','$sqlname','$sqlname2')";
$sql = mysql_query($query) or die (mysql_error());
}
echo("<form name='add' method='post' enctype='multipart/form-data' action='?action=add'>");
echo("<input type='hidden' name='MAX_FILE_SIZE' value='3000000");
echo("<input type='hidden' name='?action=add'>");
echo("<table class=main cellspacing=0 cellpadding=2 width=60%>");
echo("<tr><td>Reference: </td><td align='right'><input type='text' size=50 name='reference' value='$reference'></td></tr>");
echo("<tr><td>Category: </td><td align='center'><select name='category'><option>Occasional</option><option>Special</option><option>Figures</option><option>Novelty</option><option>Wedding</option></SELECT></td></tr>");
echo("<tr><td>Sub Category: </td><td align='center'><select name='sub_cat'><option></option><option>Birthday</option><option>Anniversary</option><option>Christmas</option><option>Christening</option><option>Valentine</option><option>Mothers Day</option><option>Fathers Day</option><option>Easter</option></select></td></tr>");
echo("<tr><td>Cake Image: </td><td align='center'><input id='file' type='file' name='file'></td></tr>");
echo("<tr><td>Cake Image 2: </td><td align='center'><input id='file2' type='file' name='file2'></td></tr>");
echo("Before uploading an image please resize the image to 290px x 218px");
echo("<tr><td></td><td><div align='right'><input type='Submit'></div></td></tr>");
echo("</table>");

Link to comment
https://forums.phpfreaks.com/topic/257479-file-upload-php-mysql/
Share on other sites

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.