Help with right code for saving and retrieving in php

In need of code for browsing a file, saving in database and retrieving

Page 1 of 1

2 Replies - 1186 Views - Last Post: 10 August 2007 - 07:10 AM Rate Topic: -----

#1 BOGHOL2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 07

Help with right code for saving and retrieving in php

Post icon  Posted 09 August 2007 - 11:28 PM

I need of a php code for browsing, saving and retrieving a file/document/picture in php. This code has errors that I have failed to overcome.

<html>
 <head>
 <title>A simple file upload form</title>
 </head>
 <body>
 <form action="do_upload.php" enctype="multipart/form-data" method="POST">
 <input type="hidden" name="MAX_FILE_SIZE" value="51200">
 <p><strong>File to Upload:</strong> <input type="file" name="fileupload"></p>
 <p><input type="submit" value="upload!"></p>
 </form>
 </body>
 </html>


 <?php
 $file_dir = "\My Documents\.";
 foreach($_FILES as $file_name => $file_array) {
	echo "path: ".$file_array['tmp_name']."<br>\n";
	echo "name: ".$file_array['name']."<br>\n";
	echo "type: ".$file_array['type']."<br>\n";
	echo "size: ".$file_array['size']."<br>\n";
	if (is_uploaded_file($file_array['tmp_name'])) {
		move_uploaded_file($file_array['tmp_name'],
		   "$file_dir/$file_array[name]") or die ("Couldn't copy");
		echo "file was moved!<br><br>";
	 }
 }
 ?>



Is This A Good Question/Topic? 0
  • +

Replies To: Help with right code for saving and retrieving in php

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with right code for saving and retrieving in php

Posted 10 August 2007 - 02:57 AM

First -> What errors are you getting, we cannot help you debug if we don't know whats going on

Second -> If you're simply uploading a file why are you using foreach? foreach is for working with like Arrays, and for uploading a file you don't need an Array do you?

When uploading a single file all you really need is

/ Where the file is going to be placed 
$target_path = "your_directory/";

/* Add the filename to the path.  
Result is "your_directory/filename.extension" */
$target_path = $target_path . basename( $_FILES['your_upload_control']['name']); 
$_FILES['your_upload_control']['tmp_name']; 

$target_path = $target_path . basename( $_FILES['your_upload_control']['name']); 

if(move_uploaded_file($_FILES['your_upload_control']['tmp_name'], $target_path)) {
	echo "The file ".  basename( $_FILES['your_upload_control']['name']). 
	" has been uploaded";
} else{
	echo "There was an error uploading the file, please try again!";
} 



No need to work with an Array for a file.

Third -> You have your $target_path as \My Documents\. Are you attempting to let people upload files to your My Documents folder? This isn't going to work, the directory needs to be a directory on your web server, not your personal computer (unless they are both in the same, even then don't use My Documents).

I hope this helps you out, and gets you going in the right direction :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Help with right code for saving and retrieving in php

Posted 10 August 2007 - 07:10 AM

No need to post TWO topics.

I've close the other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1