Help modifying an existing PHP code snippet

Hope this post is not inappropriate. As far as php coding goes… i’m just about dumb as a rock. :-[

I need to modify this code. As it is now, after a file or multiple files have been uploaded successfully, the user gets a one line message saying “Your file(s) have been received.” repeated as many times as the number of files uploaded. What I need to happen after a successful upload is to have the user sent to support.jkbsystems.com in a popup window with w=1000 h=800. Can… well “can” is the wrong word… will someone help me out here?

[php]

<?php $error_message[0] = "Unknown problem with upload."; $error_message[1] = "Uploaded file too large (load_max_filesize)."; $error_message[2] = "Uploaded file too large (MAX_FILE_SIZE)."; $error_message[3] = "File was only partially uploaded."; $error_message[4] = "Choose a file to upload."; $upload_dir = './pictures/'; $num_files = count($_FILES['upload']['name']); for ($i=0; $i < $num_files; $i++) { $upload_file = $upload_dir . urlencode(basename($_FILES['upload']['name'][$i])); if (!preg_match("/(gif|jpg|jpeg|png)$/",$_FILES['upload']['name'][$i])) { print "Only file types gif, jpg, jpeg, and png allowed. Press you BACK button and try again."; } else { if (@is_uploaded_file($_FILES['upload']['tmp_name'][$i])) { if (@move_uploaded_file($_FILES['upload']['tmp_name'][$i], $upload_file)) { /* Great success... */ echo "Your file(s) have been received."; //$content = file_get_contents($upload_file); //print $content; } else { print $error_message[$_FILES['upload']['error'][$i]]; } } else { print $error_message[$_FILES['upload']['error'][$i]]; } } } ?>

[/php]

Well, you need to create a variable, let’s call it “files_uploaded” and set it to zero before you start uploading files.
Remove the message stating that a file was uploaded and in it’s place add one to the variable.

Then, after your for-loop going thru all of the files, check if the files_uploaded variable is still zero or not.
If zero, display a notice stating that no files were uploaded. If not zero, then, state that $files_uploaded number of files were
uploaded. Does that make sense?

Now, since PHP is handled SERVER-SIDE only and does not exist inside the browser, you can not easily POP-UP a page
to display things inside of. You can add code to pop-up a page using Javascript or JQuery. Not in PHP. You can redirect a
user from your PHP page to any other page in this manner:
header(“Location: www.somewhere.com/somefolder/somepage.php”);

To do a popup window, you would need the posted-to page to have a JQuery script to run when the page loads to show the
second page. This can be done in PHP. Sorry, this might be confusing you. So, you can add, using PHP a section of code
at the bottom of the page, conditionally depending on previous values of variables. This code echoed to the bottom of the
page just before the tag would be a simple command to load a second window as a separate browser window.
You can use something like this JS script:
[php]

[/php]
BUT, this will open the extra page every time you go to the page. Therefore, you would need to only add this script at the
bottom of the page when you want to make it happen. And, therefore, conditionally put this code in place using some sort
of IF clause. Loosely something like:
[php]

<?PHP if ($files_uploaded==0) { // Do not pop up the second page... } else { ?> <?PHP } ?>

[/php]
If the files are uploaded, display the second page, if not uploaded, skip it… Or show a message…

Did that make sense?

I’m a fan of the blueimp file upload plug-in on github.

You also don’t want to supress errors, if a file can’t be moved, shouldn’t the user know there was an issue?

I used that plugin once, Astonecipher, it worked nice for the client!

And, yes, I also dislike using “@” to suppress any errors. Much better to trap them and display them! Good catch!

Sponsor our Newsletter | Privacy Policy | Terms of Service