Jump to content

Passing data through a different Superglobal Variable in PHP


bsprogs

Recommended Posts

I was originally using my own upload code to allow users to upload files. This was done by passing form data to a PHP page through the superglobal variable $_FILES.

Now, I'm using a CGI/PERL uploader script, however, the PERL script passes the file information to my PHP pages through the superglobal variable $_POST.

Now here is the fun part...

 

I've got 200 lines of code with my PHP uploader (it does a lot more than just upload) but the whole script is based off of the values from $_FILES, not $_POST. The new CGI/Perl script that I want to use does not pass a few bits of information, such as file size, file type, etc.. through the $_POST variable.

 

My original script will rename the file, verify information about the file, then move it from the temp directory, the submit the database information. The new script moves the files to a pre-set directory and thats about it.

 

I know that I can adapt my PHP to take that specific file, rename it, move it, submit the database info, etc.. however, I'm slowly getting more users and as people start to use it, there is a change that two people MAY upload a file with the same name at the same time (Such as: IMG_1002.JPG from a digital camera or something).

 

What do you guys think? The chances of this happening with two files being uploaded have less than a .0001% change of happening or is this something I should be worried about?

 

I can work well with PHP and make it do what I need it to do, but at the same time, I want it done right with the least chance of error.

Link to comment
Share on other sites

Unless you're having mass amounts of uploads simultaneously, it shouldn't be too bad....

 

Worst case, I suggest coding the features of your old script your self, so that it stores the original name in a database, and then it renamed it to some random name.

Link to comment
Share on other sites

Unless you're having mass amounts of uploads simultaneously, it shouldn't be too bad....

 

Worst case, I suggest coding the features of your old script your self, so that it stores the original name in a database, and then it renamed it to some random name.

 

Right now is does just that.

 

My PHP code does the following:

 

1. File is uploaded to temp directory

2. Generates new file name

3. writes info to database

4. moves file

 

The CGI/Perl script I want to use (because of the progress upload bar) would require me to do it like so:

 

1. File is uploaded and moved via the Perl script

2. Pass $_POST information to PHP

3. Rename file

4. Write to database

5. move file again.

 

 

Thats where the problem comes in. With PHP you have the $_FILES superglobal variable which holds on to that specific file.

The Script I would like to use moves everything first and passes it as a POST method which, I believe, "drops" that specific file.

So if two people were to upload "IMG_2002.JPG" at the same time, the script does have some possibility of renaming the wrong file or having one file overwrite the other.

 

Is it possible to pass the upload information to a PHP form to process the actual upload while using AJAX to only keep an eye on the upload progress? This would eliminate the need for the Perl to actually upload the file. However, If I'm understanding how these AJAX progress uploaders work, the XMLHttpRequest gets progress status based on what has been uploaded via the perl Script, which isn't possible to do in PHP.

Link to comment
Share on other sites

Why not just modify the Perl script to give you the data you need?

 

Perl really is just like PHP, just a lot more powerful and fun. Thats my suggestion.

 

 

Yes PHP can have its own "progress bar"  google PHP Upload Progress Bar

 

http://www.google.com/search?hl=en&q=php+progress+upload+bar&btnG=Google+Search

 

I've actually been looking at PHP progress upload bar for a few weeks now. every line on the first two pages of google results are "visited" and I've looked at the results.

Each has a perl script somewhere which requires you to give a static upload directory to upload the file to before you can change information such as the file name.

 

There are a few reasons why I dont just modify the Perl script.

1. I dont know Perl really at all.

2. Once files are uploaded in my PHP script, the script actually does quite a bit with resizing thumbs, moving files, submitting multiple database queries, populating a few session variables, passing information back to the upload page, and printing out the [bB] / <html> code back to the user.

Link to comment
Share on other sites

It seems as though you are correct in a portion of it. The reason PERL is used is because PHP does not offer ample amount of file support. It has nothing to do with the xmlhttprequest portion, php can do that just fine.

 

You can Patch PHP to be able to do that information http://www.ajaxlessons.com/2006/03/13/asynchronous-file-upload-with-ajax-progress-bar-in-php/ (see there)

 

Or continue using the perl script and either modify it to get what you need or just keep on trucking.

 

As far as 2 people uploading the same image at the same time. The best way to solve this is either a, store the file on the server in an MD5 Hash of the name and the time and the username for generating the hash, and associate this hash in the database or b, you create a directory for each user (highly advise against).

 

If you generate a hash unique to the user, filename and time uploaded this problem should never occur. Than you create a php script that is like get_image.php which intakes the filename and username to grab the file hash from the db. With using mod_rewrite you can make it so that the url request is done like www.yoursite.com/username/images/image_name.jpg   

 

Fun stuff, I guess I am not really sure what you are looking for. As for getting the extra bits of information, I am sure a simple modification to that perl script would do. Like I said above, even if you know nothing about PERL but can code in PHP you will be fine (as php was originally coded in PERL they are very similar), it is just googling trying to find the functions that you need to determine the information you need. Especially since the core code is already in place.

 

If you do need more help, re-state what you are trying to get out of this post.

Link to comment
Share on other sites

I think I found my solution!

 

http://blog.joshuaeichorn.com/archives/2006/03/14/php-ajax-file-upload-progress-meter-updates/

 

I'll have my server updated with the required files in a bit. I'll post up if I can get it to work.

And I noticed that it does use the $_FILES superglobal variable and not a line of Perl code!

 

I'll let you know in a bit!

 

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.