<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>";
}
}
?>
Help with right code for saving and retrieving in phpIn need of code for browsing a file, saving in database and retrieving
Page 1 of 1
2 Replies - 1024 Views - Last Post: 10 August 2007 - 07:10 AM
#1
Help with right code for saving and retrieving in php
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.
Replies To: Help with right code for saving and retrieving in php
#2
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
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!
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!
#3
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.
I've close the other.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|