944,113 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3235
  • PHP RSS
Oct 15th, 2009
-1

Uploading files to web server...

Expand Post »
Hi there,

I have some code which quite nicely uploads PDF files to the server:

PHP Syntax (Toggle Plain Text)
  1. if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  2. //Check if the file is JPEG image and it's size is less than 350Kb
  3. $filename = basename($_FILES['uploaded_file']['name']);
  4. $ext = substr($filename, strrpos($filename, '.') + 1);
  5. if (($ext == "pdf") && ($_FILES["uploaded_file"]["type"] == "application/pdf") &&
  6. ($_FILES["uploaded_file"]["size"] < 4550000)) {
  7. //Determine the path to which we want to save this file
  8. $newname = dirname(__FILE__).'/Invoices/'.$filename;
  9. //Check if the file with the same name is already exists on the server
  10. if (!file_exists($newname)) {
  11. //Attempt to move the uploaded file to it's new place
  12. if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
  13. echo "It's done! The file has been saved as: ".$newname;
  14. } else {
  15. echo "Error: A problem occurred during file upload!";
  16. }
  17. } else {
  18. echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
  19. }
  20. } else {
  21. echo "Error: Only .pdf images are accepted for upload";
  22. }
  23. } else {
  24. echo "Error: No file uploaded";
  25. }
  26. $varname = $_POST[$newname];
  27. echo "<SCRIPT LANGUAGE='javascript'>refreshParent();</SCRIPT>";
  28. ?>

HTML:

PHP Syntax (Toggle Plain Text)
  1.  
  2.  
  3. <form enctype="multipart/form-data" action="upload.php" method="post">
  4. <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
  5. Choose a file to upload: <input name="uploaded_file" type="file" /> <br>
  6. <input type="submit" value="Upload" />
  7. </form>

What I would like to do is have the file upload, then return the file path to a textbox on the parent webpage. Any idea how to close my upload.php page and return the file path?

Thanks in advance,

Mapper99
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008
Oct 15th, 2009
0
Re: Uploading files to web server...
You already did echoed the uploaded file path on the server.
you just need to show the same as a innerHTML of the textbox.
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 15th, 2009
0
Re: Uploading files to web server...
I havent tested this, try this if its work for you -
PHP Syntax (Toggle Plain Text)
  1. if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
  2. echo "It's done! The file has been saved as: ".$newname;
  3. $str = "document.write('javascript.getElementById('txtReturnPath').innerHTML = $newname ');"
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 15th, 2009
0
Re: Uploading files to web server...
PHP Syntax (Toggle Plain Text)
  1. <form enctype="multipart/form-data" action="upload.php" method="post">
  2. <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
  3. Choose a file to upload: <input name="uploaded_file" type="file" /> <br>
  4. <input type="submit" value="Upload" />
  5. <br><br>
  6. File Path on Server: <input type="text" name="txtReturnPath" id="txtReturnPath" />
  7. </form>
  8. </body>
  9. </html>
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 16th, 2009
0
Re: Uploading files to web server...
Thanks for your help. This should work. The problem is that there are two php files being used. When I click on the upload button on my first PHP page, the new PHP page loads, executes the upload, and lets the user know what happened. I need this page to close (or, not even display at all), then update a text box on the parent page. Does this make sense?

Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
Mapper99 is offline Offline
42 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Fill a table
Next Thread in PHP Forum Timeline: Confused :(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC