Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload problem

Status
Not open for further replies.

veronika1982

Technical User
May 15, 2006
6
YU
hi all!
I made in html an upoload form well. But do not know how to write php script that could upload the file on server!
please help
I try a lot of code from the google.com but some thing wrong always.
I don't know for sure that the uploaded file goes to the data base or goes in some folder on ftp?
 
I once made a sample code for someone, based on the php.net manual, as pointed out by sleipnir214.

it's based on the example 38-3, with minor changes.

Code:
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:<br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="submit" name="action" value="Upload" />
</p>
</form>
 
<?php
if ($_POST['action'] == "Upload") {
foreach ($_FILES["pictures"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       $name = $_FILES["pictures"]["name"][$key];
       $size = getimagesize($tmp_name);
       if ($size['mime'] == "image/jpeg") {
         if (move_uploaded_file($tmp_name, "test/$name")) {
          echo "<br />file: <strong>$name</strong> uploaded!";
         }
        else {
          echo "<br />file: <strong>$name</strong> not uploaded!";
         }
       }
      else {
        echo "<br />file: <strong>$name</strong> not supported";
      }
   }
}
}

?>

Olav Alexander Mjelde
Admin & Webmaster
 
There is no hard-coded location into which PHP will always write temporary uploaded files. There is a runtime configuration option in php.ini, upload_tmp_dir, which specifies this directory.

But you don't need to worry about where PHP will put the file, as PHP will tell you if you ask it. That page to which I directed you contains the text:

$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server.

That array element will contain a full path-and-filename of to where the uploaded temporary file was written.



Want the best answers? Ask the best questions! TANSTAAFL!
 
its my upload form exemple

<html>
<head><h1>Uploadujte omiljene slike </h1>
<title>upload</title>
<body>


<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />

Send this file:<br> <input name="file" type="file" />
<input type="submit" value="Send File" />
</form>




</body>
</html>


and this is a php cod

<?php


if ($_FILES['submit']) {
print_r($_FILES);
if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
$error = "You did not upload a file!";
unlink($_FILES['file']['tmp_name']);
// assign error message, remove uploaded file, redisplay form.
} else {
//a file was uploaded
$maxfilesize=10240;

if ($_FILES['file']['size'] > $maxfilesize) {
$error = "file is too large";
unlink($_FILES['file']['tmp_name']);
// assign error message, remove uploaded file, redisplay form.
} else {
if ($_FILES['file']['type'] != "image/gif" AND $_FILES['file']['type'] != "image/pjpeg") {
$error = "This file type is not allowed";
unlink($_FILES['file']['tmp_name']);
// assign error message, remove uploaded file, redisplay form.
} else {
//File has passed all validation, copy it to the final destination and remove the temporary file:
copy($_FILES['file']['tmp_name'],"/finallocation/".$_FILES['file']['name']);
unlink($_FILES['file']['tmp_name']);
print "File has been successfully uploaded!";
exit;
}
}
}
}



?>

what is wrong here?
 
I don't understand. What do you mean by "what is wrong"?

Get rid of these lines:

copy($_FILES['file']['tmp_name'],"/finallocation/".$_FILES['file']['name']);
unlink($_FILES['file']['tmp_name']);

and replace them with:

move_uploaded_file($_FILES['file']['tmp_name'],"/finallocation/".$_FILES['file']['name']);

move_uploaded_file() has additional protection built-in to prevent your code from accidentally moving the wrong file.

However, that destination path doesn't look right. You have to keep in mind that PHP's filesystem functions, of which copy() and move_uploaded_files() are two, are not constrained by the document root of the current web site. They can operate anywhere on the filesystem. So when you use a destination of
"/finallocation/".$_FILES['file']['name']
you're probably putting the file somewhere you do not expect. You need to specify the absolute pathname to the desination, as the example code on the "Handling file uploads" page indicates.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top