Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I know there are several image tutorials, however I am having an issue with multiple image upload that I cannot see an answer to. I would GREATLY appreciate any assistance.
I have a page where the user inputs info that gets inserted into the db. Also, on the same page, the user can upload up to three images. Everything works great (all info is inserted, images go to correct folder, correct image name goes into db) HOWEVER, you HAVE to upload all 3 images for it to work properly. All 3 fields may not all have images all of the time. I have the images using the time in the filename when uploading so the names are unique and if you do not have an image in the upload field, just the time stamp and image directory name is inserted into the db as the file name. I have tried using if statements to verify that there is a file in the upload field but have failed miserably with all attempts. I am at a novice to intermediate level in my coding. I did find some of this code online. Below is the code:
[syntax="php"]
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'files/';
// make a note of the location of the upload form in case we need it
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'pubs_view.php?action=add';
// make a note of the location of the success page
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload_success.php';
// fieldname used within the file <input> of the HTML form
$cover = 'cover';
$pubtitle = 'pubtitle';
$sample = 'sample';
// Deal with upload
// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');
// check the upload form was actually submitted else print the form
isset($_POST['submit'])
or error('the upload form is neaded', $uploadForm);
// check that the file we are working on really was the subject of an HTTP upload - I commented this out for troubleshooting
// @is_uploaded_file($_FILES[$cover]['tmp_name'])
// or error('not an HTTP upload', $uploadForm);
// make sure it's an image - I commented this out for troubleshooting
// getimagesize() returns false if the file tested is not an image.
// @getimagesize($_FILES[$cover]['tmp_name'])
// or error('only image uploads are allowed', $uploadForm);
// make a unique filename... if it is already taken keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$cover]['name']))
{
$now++;
}
while(file_exists($uploadFilename2 = $uploadsDirectory.$now.'-'.$_FILES[$pubtitle]['name']))
{
$now++;
}
while(file_exists($uploadFilename3 = $uploadsDirectory.$now.'-'.$_FILES[$sample]['name']))
{
$now++;
}
//move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$cover]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
@move_uploaded_file($_FILES[$pubtitle]['tmp_name'], $uploadFilename2)
or error('receiving directory insuffiecient permission', $uploadForm);
@move_uploaded_file($_FILES[$sample]['tmp_name'], $uploadFilename3)
or error('receiving directory insuffiecient permission', $uploadForm);
//create filename for db
$dbfilename = $now.'-'.$_FILES[$cover]['name'];
$dbfilename2 = $now.'-'.$_FILES[$pubtitle]['name'];
$dbfilename3 = $now.'-'.$_FILES[$sample]['name'];
//declare values for add
$SQL = "UPDATE publications SET ";
$SQL .= "title = '" . $_POST['title'] . "', ";
$SQL .= "compiler = '" . $_POST['compiler'] . "', ";
$SQL .= "description = '" . $_POST['description'] . "', ";
$SQL .= "nprice = '" . $_POST['nprice'] . "', ";
$SQL .= "mprice = '" . $_POST['mprice'] . "', ";
$SQL .= "intro = '" . $_POST['intro'] . "', ";
$SQL .= "cover = '" . $dbfilename . "', ";
$SQL .= "pubtitle = '" . $dbfilename2 . "', ";
$SQL .= "sample = '" . $dbfilename3 . "' ";
$SQL .= "WHERE ID = '" . $_POST['ID'] . "'";
If there is someplace where this is listed, I apologize in advance and would appreciate a link. Thanks!
pickle | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]