Page 1 of 2

Appending uploaded files

Posted: Mon Jun 09, 2003 1:35 pm
by mesz
I am using a form to upload image files.
This works.
But each new image overwrites the last one.
How do I keep adding to the file?
I know it has something to do with append..I can make this work with text but not images.
Please help.

Posted: Mon Jun 09, 2003 2:56 pm
by Galahad
I don't think that you want to append one image onto another. They probably need to be separate files. What are you trying to do with this page?

How are you handling the upload? If you use move_uploaded_files (I think it is really strongly recommended that you use this function), just tell it to move it to a different filename. Read about it here. The php manual includes a pretty good example of how to do uploads. Hope that helps.

Posted: Tue Jun 10, 2003 5:50 am
by mesz
Galahad, cheers for your reply.
I do not think that I have been eloquent enough in my explanation however.
I do not want to merge images into one another, but rather build up a flat file database.
I am not trying to be cheeky and get a free photo gallery script posted here - honest - I'm just stuck.
Like I say I can create a news system using append so that the newest entrys from a text field ( via a form ) are added to text file and displayed on an html page.
However as soon as I use move_uploaded_file I run into difficulty and I do not seem to be able to use append. Each image that is uploaded from a users computer simply replaces the previous in my file ( a html page set to chmod 777 ).
Are you suggesting that each image has to be uploaded to a new blank document - all the blank documents would then have to be displayed together on another page via an include.
This sounds more complicated than I imagined - does anybody know how to add new images to a file whilst retaining the previous ones via a client side addition.
Cheers for any help you can offer - I'm going slowly mad here.

Posted: Tue Jun 10, 2003 6:06 am
by patrikG
Can you post the form and the upload-script?

Posted: Tue Jun 10, 2003 6:25 am
by mesz
Here is the file upload script ( obvious minus the append facility - i.e. this version at least works! ):

Code: Select all

<html> 
   <body> 
<? 
if (is_uploaded_file($f1)) { 
  move_uploaded_file($f1, "file.html"); 
} 
?> 
<?php 
   if (count($_FILES) > 0) 
   { 
?> 
      <fieldset><legend>contents of array $_FILES</legend> 
         <pre><?php print_r($_FILES); ?></pre> 
      </fieldset> 
<?php       
   } 
 if (count($_POST) > 0) 
   { 
?> 
      <fieldset><legend>contents of array $_POST</legend> 
         <pre><?php print_r($_POST); ?></pre> 
      </fieldset> 
<?php       
   } 
?> 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post"> 
         <p> 
            <input type="file" name="f1" /> 
            <input type="submit" /> 
         </p> 
      </form> 
   </body> 
</html>
file.html is a blank document - as I say set to chmod 777.
The directory that these pages are stored in is writeable to as well.
Cheers for your interest in this problem.

Posted: Tue Jun 10, 2003 11:26 am
by Galahad
Try something like:

Code: Select all

<?php
$imagesdir = "./images/";

if (isset($_FILES['f1'])) {
  // A file has been uploaded through the form
  if (!move_uploaded_file($_FILES['f1']['tmp_name'], $imagesdir . $_FILES['f1']['name'])) {
    // Insert error code here
  } else {
    // Insert success code here
  }
} else {
  // Display your upload form here
}
?>
That way as long as each file on your machine has a unique name, each file on the server will have a unique name. Then you could have another page (view.php or something) that reads the contents of the images directory (using the directory functions) and displays them all in a nice html page. Let us know if you need further explanation / help.

Posted: Tue Jun 10, 2003 11:49 am
by mesz
Cheers for your help - it is genuinely appreciated.
However I am useless, I need a little more...
So far I have:

Code: Select all

<html> 
   <body> 
<?php 
$imagesdir = "./pic/"; 

if (isset($_FILES['f1'])) { 
  // A file has been uploaded through the form 
  if (!move_uploaded_file($_FILES['f1']['tmp_name'], $imagesdir . $_FILES['f1']['name'])) { 
    echo ' error ';
  } else { 
    // Insert success code here 
  } 
} else { 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post"> 
         <p> 
            <input type="file" name="f1" /> 
            <input type="submit" /> 
         </p> 
      </form> 
} 
?> 
   </body> 
</html>
However I do not know what the success code should be - is this move the uploaded file or using fwrite to write th efile to somewhere - as I have said these uploaded files have got my head so mashed I can't tell which way is up at the moment.

and view.php

Code: Select all

<?php
if ($handle = opendir('pic')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
    while (false !== ($file = readdir($handle))) { 
        echo "$file\n";
    }
    closedir($handle); 
}
?>

Posted: Tue Jun 10, 2003 12:31 pm
by Galahad
The success code may be something like displaying a message "Your file has been uploaded successfully" and perhaps a link to upload a new file and/or view the gallery. The file is actually moved in this line:

Code: Select all

if (!move_uploaded_file($_FILES&#1111;'f1']&#1111;'tmp_name'], $imagesdir . $_FILES&#1111;'f1']&#1111;'name'])) &#123;
From a quick glance, your view.php page looks like it will work. It won't show the actual image, just display the filename. That's great just for testing, but when you are ready to show the actual image, you'll want to replace:

Code: Select all

echo "$file\n";
with something like:

Code: Select all

echo "&lt;img src='$imagesdir$file' /&gt;\n";
That may require some tweaking, but something to that effect should work.

I forgot about something important. Perhaps you'll want to do this after you get it working a little, but you probably should check for an error code on the upload. $_FILES['f1']['error'] gets set to be an error number if one occurred or 0 if it was ok. Basically you'd want something like:

Code: Select all

<?php
if (isset($_FILES['f1'])) {
  // A file has been uploaded through the form

  // Insert upload error checking code here  

  if (!move_uploaded_file($_FILES['f1']['tmp_name'], $imagesdir . $_FILES['f1']['name'])) {
// Rest of code here
?>
Look at the first comment on this page to see some sample code for that. The editor of the manual comments notes that instead of '0' the poster should have used 'UPLOAD_ERR_OK' and so on for the other values. However, you could try to get what you have to work, then worry about errors.

Posted: Tue Jun 10, 2003 12:39 pm
by mesz
Just trying now - I do not have PHP installed on the computer I am sat at so I am having to uplaod the php pages to my host via FTP to see if this works!
If it does this will be good!
Cheers for getting back to me so quickly.

Posted: Tue Jun 10, 2003 12:45 pm
by mesz
This is the file upload script I am trying now:

Code: Select all

<html> 
   <body> 
<? 
$imagesdir = "pic"; 
if (isset($_FILES['f1'])) { 
  if (!move_uploaded_file($_FILES['f1']['tmp_name'], $imagesdir . $_FILES['f1']['name'])) { 
    echo ' error ';
  } else { 
   echo ' yo my fool - you have uploaded successfully - you should have a biscuit';
  } 
} else { 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post"> 
            <input type="file" name="f1"> 
            <input type="submit"> 
      </form> } 
?> 
   </body> 
</html>
And this is the error I am getting:
Parse error: parse error, unexpected '<' in /home/.sites/14/site467/web/pic/f2.php on line 12

Is the form no longer supposed to refer to this page?

Posted: Tue Jun 10, 2003 1:06 pm
by Galahad
You need to close off the php section before your html form, so the <form> tag will be interpreted as plain html.

Posted: Tue Jun 10, 2003 1:21 pm
by mesz
Cheers again - sorry this is turning into such hard work.
( I found building an online shop using javascript less painful than this ).

Posted: Wed Jun 11, 2003 8:11 am
by mesz
My view.php code is like this:

Code: Select all

<?php 
if ($handle = opendir('./images/')) { 
    echo "Directory handle: $handle\n"; 
    echo "Files:\n"; 
    while (false !== ($file = readdir($handle))) { 
        echo "<img src='$imagesdir$file' />\n";  
    } 
    closedir($handle); 
} 
?>
However only 5 small boxes with red crosses ( presumably the standard html missing image ident ) are displayed - do you know why?
I'm going to go throughthis forum looking for answers - as google have not been able to help!

Posted: Wed Jun 11, 2003 8:26 am
by twigletmac
When you right click on the boxes and go to properties, what does it tell you about the image it is trying to display?

Mac

Posted: Wed Jun 11, 2003 8:52 am
by mesz
I get the full URL of the image
Protocol : http
URL : http://www.distortedmedia.co.uk/pic/arrowz.gif
Type: not avaliable
Size: not avaliable
Dimensions: 28 * 30 pixels
Created / Modified - no info