Thats pretty cool thanks.
So in my case how I would it from inserting info into the db for all upload boxes when I only want to upload one image and not three of them. Is there a loop or something that can do this?
| SitePoint Sponsor |
Thats pretty cool thanks.
So in my case how I would it from inserting info into the db for all upload boxes when I only want to upload one image and not three of them. Is there a loop or something that can do this?


Do you mean how could you stop it adding 3 records into the database if you have only selected 1 image?
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!


I guess thats what you mean so....
After this line:
add another check to see if the error code is there, just as you did earlier in the script.PHP Code:for($i=0; $i<=($numberOfUploads-1); $i++){
PHP Code:for($i=0; $i<=($numberOfUploads-1); $i++){
if($_FILES['postPIC']['error'][$i] != 4) {
// carry on the processing and database insert
} // dont forget the closing brace at the end of the processing!
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
Yes that is what I mean, sorry for the confusion.
If I only select one image then I only want it to insert the info in the db for that image.
That works perfectly. Your a great help thanks so much.
How did you learn so much about PHP? How long has it taken you to get to be so good at it?


Started off in about 2004 just before I joined Sitepoint - in fact that was the reason for joining!
The thing with php is that it is quite easy to get to grips with the basic functions and what they do, then it;s a case of putting it all together with a little help here and there![]()
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
This is interesting post because I'm having the same problem with uploading multiple images.
I'm not sure how to print out the images from database when using array - do I need to change headers or use the array somehow in the printing process?
I'm using following code to print one image:
header('Content-Length: '.strlen($pic));
header("Content-type: image/{$type}");
echo $pic;
How should I change it when I have multiple images stored in array and I want to print them all?


Why are they stored in an array ggnome? have you pulled them out of the database and then into an array or is this on the upload part?
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
I'm storing images as binarydata (BLOB) into table. I have only one row called 'data' to store the images. I'm confused if I should have row for every image I upload?
The problem is I don't understand how the uploaded data is stored in the table and how to pull it out..
I'm quite new with php and mysql so I'm sometimes confused with the basics =)


Well there are 2 ways to handle file uploads, you have done one and the other is to simple upload the file to it's destination directory and store the filename in the database rather than the full image data.
Makes life much easier!
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!


I was working through your example trying to apply it to my upload script but it line above throws an error for me. Any chance you could help me out?
I don't think Im applying the looping around my function correctly:
PHP Code:<?
$uploadDir = $_SERVER['DOCUMENT_ROOT'].'/slideshow/datafiles/';
// if upload ...
if(isset($_POST['upload']))
{
$tmp_pic_array = array();
$tmp_pic_array = $_FILES['userfile']['tmp_name'];
// count the number of $_FILES sent
$numberOfUploads = count($_FILES);
if($numberOfUploads == 0) {
echo '<p>You selected not to add a picture to this post.</p>';
} else {
for($i=0; $i<=$numberOfUploads; $i++){
$fileName = $_FILES['userfile']['name'][$i];
$tmpName = $_FILES['userfile']['tmp_name'][$i];
$fileSize = $_FILES['userfile']['size'][$i];
$fileType = $_FILES['userfile']['type'][$i];
}
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include 'library/config.php';
include 'library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload (files_name, files_userid, files_size, files_type, files_path ) ".
"VALUES ('$fileName', '$userID', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'library/closedb.php';
echo "<br>File uploaded<br>";
}
?>
Bookmarks