How can I reference a file permanently once it's uploaded and moved from tmp to a permanent location?
February 27, 2004 6:38 PM   Subscribe

Headaches of permanently pointing to an uploaded file via PHP (more inside, thank you very much).

Uploading the file (e.g. image), grabbing the file name, and moving it from the tmp to a permanent location has been no problem. It's when I want to reference that same file permanently that I run into roadblocks. How does someone use a variable pulled from the initial form and reference it again? Is this a database thing (I dont want to worry about databases right now)? Cookies? I've searched php.net and google for a simplistic answer to this question and have only run into complicated scripts that are well beyond my comprehension. All I want is a super simple example of how to upload a single file and view it on the same page permanently. I apologize in advance if this request is juvenile:)
posted by poopy to Computers & Internet (5 answers total)
 
It really depends on the intended effect you're looking for. From what I gather, your process is:

1. User accesses a page that displays an image and provides a form, via which they can upload a new image file.

2. User utilizes the form to upload a new image file. The PHP then moves the file from its temporary position into a permanent Web-accessible folder.

3. The new image is now displayed on the original page.

Question: Do you want an archive of images? (i.e. Should each uploaded image have a unique filename and only the most recent image will be displayed?) Or could you theoretically overwrite the image whenever a new one is uploaded?

Solution: If you only want to keep the current image, just overwrite the old image as a new one is uploaded. If each image has the same filename (i.e. recent.jpg), you could simply refer to <img src="recent.jpg"> in your HTML and the latest image would always be displayed.

If each file should have a unique filename, however, you will have to do a bit more work. The simplest method would be to create a plaintext file that contains the filename of the most recent image, which would then be include()'d into your page. You would update this plaintext file during the upload procedure.

<?php
// This would be executed in the procedure you use to move the file
// from its temporary to its permanent position. $filename is the
// filename you have assigned the image.

$filehandle = fopen('recent.txt', 'w');
fwrite($filehandle, $filename);
fclose($filehandle);
?>

And this is the HTML code that displays the image in question:

<html>
<body>
<img src="<?php include('recent.txt'); ?>">
</body>
</html>

You may run into some problems with file permissions and PHP (unless your server's running php-cgi), but since you seem to have conquered saving uploaded images into directories, you may not.

One other thing you'll want to do before moving an uploaded image to its permanent location is check to make sure it's an image. You can do this using the is_uploaded_file() and getimagesize() functions. If the uploaded file has a valid width and height, it's likely an image. If you don't perform these checks on files, who knows what the user could attempt to upload, and which PHP bug they could attempt to exploit to execute arbitrary code on your server.
posted by Danelope at 7:18 PM on February 27, 2004 [1 favorite]


I'm going to try to rephrase the question and let me know if this is what you had in mind:

You have a PHP script that you use to upload files to your webserver. The script moves the temporary file created by PHP to a more permanent location, and names it according to what you put in the file input field. Your problem is that you want to write a script that references all those uploaded files, but you don't know the names of those files ahead of time.

Depending on your OS, you can use the either the "dir" command in windows or the "ls" command in unix/linux to list the contents of a directory. To pull the output of one of those commands, you can do something like this (linux):

$filenames = `ls -1 /path/to/permanentUploadDirectory`

or for windows:

$filenames = `dir /b c:\\path\\to\\permanentUploadDirectory`

So $filenames will be a string with all the filenames in it, one per line. You can it into an array with:

$fileArray = split("[\r\n]", $filenames);

Then you can loop through the array to output HTML to display the images or whatever you need to do.
posted by mfbridges at 7:27 PM on February 27, 2004


NOTE: Those are backticks (`), not single quotes (') in the above example.
posted by mfbridges at 7:30 PM on February 27, 2004


"I dont want to worry about databases right now"

Then what will you be using to store the name of the file?

You can do one of three things (off the top of my head):

1) Give the file whatever name you want. You can create this for each file. Then add to an include file that will be made up of links to the files. Like Danelope said.

2) Upload the image and store the name of the file as it's entered in the form.

3) Have another form element that will let the user specifya name.

Which one of these are you thinking of?

Personally I'd store the file name, location and details in a database.
posted by y6y6y6 at 10:13 PM on February 27, 2004


thanks all. actually, Danelope is right on, and thanks a bunch for that advice! y6y6y6, the reason i'm not worrying about databases right now is that i'm trying to figure the bare basics of PHP before i delve into that. maybe this isn't the right way to go, but it feels right to me. i want to take it one step at a time (especially because i'm no programmer of any stretch) and i've was doing pretty well (thanks to yours and others previous advice) up till now.
posted by poopy at 6:57 AM on February 28, 2004


« Older exact quote re copyright from a SCOTUS judge   |   What's Benign About Benign Tumors? Newer »
This thread is closed to new comments.