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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.