Appending uploaded files
Moderator: General Moderators
Appending uploaded files
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.
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.
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.
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.
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.
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.
Here is the file upload script ( obvious minus the append facility - i.e. this version at least works! ):
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.
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>The directory that these pages are stored in is writeable to as well.
Cheers for your interest in this problem.
Try something like:
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.
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
}
?>Cheers for your help - it is genuinely appreciated.
However I am useless, I need a little more...
So far I have:
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
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>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);
}
?>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:
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:
with something like:
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:
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.
Code: Select all
if (!move_uploaded_file($_FILESї'f1']ї'tmp_name'], $imagesdir . $_FILESї'f1']ї'name'])) {Code: Select all
echo "$file\n";Code: Select all
echo "<img src='$imagesdir$file' />\n";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
?>This is the file upload script I am trying now:
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?
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>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?
My view.php code is like this:
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!
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);
}
?>I'm going to go throughthis forum looking for answers - as google have not been able to help!
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
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