File Upload Situation...
Posted: Sun Aug 09, 2009 11:34 am
Hello everyone.
I am having a problem with a file upload. The uploading of the file works great. I want my user to either select a file (that is already stored in the system) from a dropdown list OR upload a file if they don't see their file in the list. The problem that I am having is:
1) If the file is selected from the dropdown list, the value of the selection is not entering into the DB.
2) If no file is uploaded then it echos an error stating that "No file was uploaded". I don't want the file upload to be required.
Here is my code:
As you can see, my logic is that if a file is not uploaded then the variable $i will be the posted value (file name) of the selection made in the dropdown menu called "logo", else when a file is uploaded then the variable $i becomes the file name of the uploaded file. So, that when I do the call to the DB it will insert the value of $i to the correct field in the DB.
If anyone can see where I am going wrong with this, your help will be greatly appreciated!!!
Shannon
I am having a problem with a file upload. The uploading of the file works great. I want my user to either select a file (that is already stored in the system) from a dropdown list OR upload a file if they don't see their file in the list. The problem that I am having is:
1) If the file is selected from the dropdown list, the value of the selection is not entering into the DB.
2) If no file is uploaded then it echos an error stating that "No file was uploaded". I don't want the file upload to be required.
Here is my code:
Code: Select all
if (!isset($_FILES['upload'])) {
$i = mysqli_real_escape_string($dbc, $trimmed['logo']);
}else{
$allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/jpg', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png', 'image/gif', 'image/GIF');
if (in_array($_FILES['upload'] ['type'], $allowed)) { // Move the file over
if (move_uploaded_file($_FILES['upload']['tmp_name'],"../uploadedimages/{$_FILES['upload']['name']}")){
$i = $_FILES['upload']['name'];
} // End move if
}else{
echo '<p class="error">Please upload a JPG, PNG, or GIF file type</p>';
}
}
// Error Checks
if ($_FILES['upload']['error'] > 0 ){
echo '<p class="error"> The file could not be uploaded becuase: ';
switch($_FILES['upload']['error']){
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error has occurred.';
break;
}// END switch
echo '</p>';
}
if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){
unlink($_FILES['upload']['tmp_name']);
}
//DB insertion code follows this...
If anyone can see where I am going wrong with this, your help will be greatly appreciated!!!
Shannon