Jump to content

Some help with uploading a file


yandoos

Recommended Posts

Hello

 

I really need some help with uploading an image. The name of the image is stored in the appropriate field and the actual image is uploaded to the directory. It works fine at the moment but that is because there is only 1 field in the table for the image. However I now have 3 image fields per record and can't work out how to upload an image to each of these.

 

So where the table had a field for image it now has image1, image2, image3. I've tried changing the following:

$fname=($_FILES['photo']['name']);

to

$image1 =($_FILES['photo']['name']);
$image2 =($_FILES['photo']['name']);
$image3 =($_FILES['photo']['name']);

But this just seems to conflict and doesnt work.

 

Any help with this would be great.

 

Here is my code that works for just a single image.

<?php 
require_once 'db_inc.php';
//This is the directory where images will be saved 
$target = "product_images/"; 
    if(!is_dir($target)) mkdir($target);
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$price=$_POST['price']; 
$stock=$_POST['stock']; 
$fname=($_FILES['photo']['name']); 
$tmpName  = $_FILES['photo']['tmp_name'];
$fileSize = $_FILES['photo']['size'];
$fileType = $_FILES['photo']['type'];



//process the file
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc()){
$fname = addslashes($fname);}


//Writes the information to the database 

mysqli_query($connection,"INSERT INTO product (productname,description,price,image1,stock) VALUES ('".$name."', '".$description."', '".$price."', '".$fname."', '".$stock."')") ; 
mysqli_close($connection);
 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 

 //Tells you if its all ok 
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
       } 
  else { 

         //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
    } 
     ?> 

Thank you

 

Link to comment
Share on other sites

I would add that I can easily change the select query appropriately so that is not the issue:

mysqli_query($connection,"INSERT INTO product (productname,description,price,image1,image2,image3,stock) VALUES ('".$name."', '".$description."', '".$price."', '".$image1."', '".$image2."', '".$image3."', '".$stock."')") ; 
mysqli_close($connection);
Link to comment
Share on other sites

Are you getting any error from your query? Do you have php error checking turned on?

 

I've not done it so I have to ask - are you sure that you can upload multiple images using the array format for the name attribute in your html? If you can you need to add another index to that name, [0],[1],[2] I think.

 

And finally - storing of images in a db is not the recommended way. An image is a large object that can stand on its own as long as you know the name (which you do) and the location (which you do). One simply grabs it for display purposes using the info in the db that points to it. No need for the overhead of storing and retrieving it from the db.

Link to comment
Share on other sites

So now you can see how the reference to the multiple files fields should be done, thanks to cyberRobot's post. It still doesn't change the fact that once you get these images you shouldn't be storing them in a table, but rather in a folder for direct reference when needed. Design your own storage scheme and stick to it.

Link to comment
Share on other sites

Thank you for the info. Just to confirm this does not store the actual image in the db but rather just the name. The actual image is uploaded to a folder on the server and when I select the field I simply point in the location of image and the image name from the db.

 

I will go through the manual and post my findings.

 

Thank you very much.

Link to comment
Share on other sites

Hello, I've managed to get it working now - it uploads 3 files and stores the name in the db. There is one problem though if I don't chose an image for 1 of the 3 images then an empty string is updated in the db name and so those image names are lost.

 

I need to some how find if the file upload is empty and if so not update that image name into the db. I'm really not sure how to do this though. Here is my code:

 <form enctype="multipart/form-data" action="update_image.php" method="POST">

<input name="photo" type="file" style="width:130px; font-size:9px;margin:0px;">   
<input name="photo1" type="file" style="width:130px; font-size:9px;margin:0px;">
<input name="photo2" type="file" style="width:130px; font-size:9px;margin:0px;">
	               
<input name="pid" type="hidden" value="<?php echo $builder1['pid'];?>">	   
Name:  <input name="productname" type="text" value="<?php echo $builder1['productname'];?>"><br/>
Price: <input name="price" type="text" value="<?php echo $builder1['price'];?>"><br/>
Stock: <input name="stock" type="text" value="<?php echo $builder1['stock'];?>"><br/>
Description:<br/><textarea name="description" cols="40" rows="8"><?php echo $builder1['description'];?></textarea>    

<input name="submit" type="Submit" value="Edit Product"/>		 
</form> 

Here is the update_image.php code:

<?php 
ini_set('display_errors', 1); 
error_reporting(E_ALL);
include "include/session.php";
  require('config.php'); 
  require('include/functions.php'); 
  $email = $_SESSION['mem_id'];
  header("Location: http://myweb.co.uk/products.php");

if (isset($_POST['submit'])) { 

$pid = $_POST['pid']; 
$productname = $_POST['productname'];
$description = $_POST['description'];
$price = $_POST['price'];
$stock = $_POST['stock'];

$target = "images/";  $target = $target . basename( $_FILES['photo']['name']);
$target1 = "images/";  $target1 = $target1 . basename( $_FILES['photo1']['name']);
$target2 = "images/";  $target2 = $target2 . basename( $_FILES['photo2']['name']);

$pic=($_FILES['photo']['name']);
$pic1=($_FILES['photo1']['name']);
$pic2=($_FILES['photo2']['name']);


$connection = mysqli_connect($dbhost_name, $username, $password, $database);
$sql = mysqli_query($connection,"UPDATE product SET productname = '$productname', description = '$description', price = '$price', stock = '$stock', image1 = '$pic', image2 = '$pic1', image3 = '$pic2' WHERE pid = '$pid'" ) ; 

$error = false;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))  
{   
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))  
{   
echo "The 2 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true; 

if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))  
{   
echo "The 3 file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";  
} else $error = true;

if($error)
   echo "Sorry, there was a problem uploading your file.";

  }

?>

How can I achieve this please???

 

Thank you :)

Edited by yandoos
Link to comment
Share on other sites

The only way I can think of doing this is using the following if else statements but there must be a better way:

if ($pic == NULL && $pic1 != NULL && $pic2 != NULL) {

} 
else if ($pic == NULL && $pic1 == NULL && $pic2 != NULL) {

}

else if ($pic == NULL && $pic1 == NULL && $pic2 == NULL) {

}
else if ($pic != NULL && $pic1 == NULL && $pic2 == NULL) {

}

else if ($pic != NULL && $pic1 != NULL && $pic2 == NULL) {

}

else if ($pic != NULL && $pic1 != NULL && $pic2 != NULL) {

}
Link to comment
Share on other sites

Have you looked into looping through the results in $_FILES? Note that there are a number of examples in the User Contributed Notes found here:

http://php.net/manual/en/features.file-upload.multiple.php#usernotes

 

Basically, you could loop through the files and test the file name as you go. Whenever you run into a file name that isn't set to NULL, process the upload.

Edited by cyberRobot
Link to comment
Share on other sites

several of the possible upload errors will result in an empty ['name'] element. to specifically test if the file upload field was left empty, you need to test for error value 4 = UPLOAD_ERR_NO_FILE

 

you would need to dynamically build the UPDATE query statement to leave out any imagex = '$picx' terms when no image was uploaded for that specific image.

Edited by mac_gyver
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.