0

Hi,

I'm trying to learn how to create an upload file form. I'm stuck on a couple of things. This is something I learn in the past but I forgot how to create one, so it's new to me now. I got the script from http://www.w3schools.com/php/php_file_upload.asp

1) I want to learn how to upload a file to a specific folder but having an issue understanding how to do that.
2) How do I override a file that already exist in that folder.

Here is the Form file:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html> 

Here is the Upload Script file

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> 

I really appreciate if someone explain to me how to write it correctly. Thanks!

4
Contributors
11
Replies
12
Views
5 Years
Discussion Span
Last Post by LastMitch
Featured Replies
  • 2

    That means that `$_FILES["pictures"]["error"]` is not an array. Read More

  • 2

    @lastMitch, Here is a sample script I posted months back. This one is a multi-file uploader, but you can easily remove the form elements that you don't need.. **!DISCLAIMER! HTML is not my strongest area in any of the coding languages.** filename: upload.php <html> <head> </head> <body> <table width="500" border="0" … Read More

  • 1

    simple explantion on what is going in the server once the upload.php is submitted. 1. PHP will save the files in the tmp directory. This is above the public directory e.g. public_html or htdocs. 2. Once the files has been uploaded successfuly, the process.php will then execute the move_uploaded_file($_FILES['ufile']['tmp_name'][0], $item1); … Read More

  • **@veedeoo** So for replying so late. I had something to do the past few days. So the past hour I finally got a chance to test out your script and it work! It was very simple for me to understand it and also your explanation on how the > move_uploaded_file … Read More

0

if my memory doesnt fail me, an uploaded file is deleted when the script is done.

you need to move or copy it to a secure folder.
i use rename($tmp_path, $secure_path)//double task, rename and move
or you can use copy($tmp_path, $secure_path)

rename is similar to move_uploaded_file($tmp, $secure) which i didnt know and will probly use in the future, thx pritaeas

and you might want to read on script injection and add some screening filters in there if you plan to put that up on the web.

Edited by pritaeas: Added inline code tags for readability

0

@pritaeas

Thanks for the reply! Thanks for the link also. I will read & look closely at the manual & make some adjustments. If I come accross an issue. I will post it. Thanks.

0

@DarkMonarch

Thanks for the reply! Thanks for the example and explanation. I will probably read that link that pritaeas provided. I will try to used those adjustments that you post on the script. If I come accross an issue. I will post it. Thanks.

0

@pritaeas

Thanks for the link, it was very informative. I look at the link and used the Example 1 and I create a POST method. An error came up after the I create a POST method.

<?php 
if ($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}else{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];}

$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

-

<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" value="Send" />
</p>
</form>

-
I connect to my database. I upload it to my server. I get this error Invalid argument supplied for foreach() in

foreach ($_FILES["pictures"]["error"] as $key => $error) 

Can you explain to me what I did wrong or explain how I got that error? I really appreciated. Thanks!

Edited by LastMitch: grammer

2

That means that $_FILES["pictures"]["error"] is not an array.

Votes + Comments
Thanks for the explaining the error
0

@pritaeas

Thanks for the explanation. So I need to create an array. I will do that, if I have any questions I will post it here. Thanks.

2

@lastMitch,

Here is a sample script I posted months back. This one is a multi-file uploader, but you can easily remove the form elements that you don't need..

!DISCLAIMER! HTML is not my strongest area in any of the coding languages.

filename: upload.php

<html>
<head>
</head>
<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="process.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Type Name:<input type ="text" name="poster"/></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

filename: process.php . If will be processing single file upload, just remove the part where it is processing the 2nd and the 3rd file. That shouldn't be too hard to accomplished.

<?php
## deviced by veedeoo, and does not have any warranty of any kind.
## not intended in production server, UNTIL all possible security measures are implemented.

$poster = $_POST['poster'];
## define destination directory

define("DIR_","uploads/");

$item1= DIR_.$poster."_".$_FILES['ufile']['name'][0];
$item2= DIR_.$poster."_".$_FILES['ufile']['name'][1];
$item3= DIR_.$poster."_".$_FILES['ufile']['name'][2];


## use move_uploaded file function
move_uploaded_file($_FILES['ufile']['tmp_name'][0], $item1);
move_uploaded_file($_FILES['ufile']['tmp_name'][1], $item2);
move_uploaded_file($_FILES['ufile']['tmp_name'][2], $item3);

echo '<img src="'.$item1.'" width="150" height="150"><br/>';
echo "File Name :".$_FILES['ufile']['name'][0]."<br/>";
echo "File Size :".$_FILES['ufile']['size'][0]."<br/>";
echo "File Type :".$_FILES['ufile']['type'][0]."<br/>";


echo '<img src="'.$item2.'" width="150" height="150"><br/>';
echo "File Name :".$_FILES['ufile']['name'][1]."<br/>";
echo "File Size :".$_FILES['ufile']['size'][1]."<br/>";
echo "File Type :".$_FILES['ufile']['type'][1]."<br/>";


echo '<img src="'.$item3.'" width="150" height="150"><br/>';
echo "File Name :".$_FILES['ufile']['name'][2]."<br/>";
echo "File Size :".$_FILES['ufile']['size'][2]."<br/>";
echo "File Type :".$_FILES['ufile']['type'][2]."<br/>";


##  show error or success.

$checkSize1=$_FILES['ufile']['size'][0];
$checkSize2=$_FILES['ufile']['size'][1];
$checkSize3=$_FILES['ufile']['size'][2];

if($checkSize1 && $checkSize2 && $checkSize3 != 0)
{
echo "Files uploaded.";
}

else {
echo "ERROR!.....";
}

## error defined

if($checkSize1==0) {
echo "There is an error in your first file. <br/>";

}

if($checkSize2==0) {
echo "There is an error in your second file.<br/>";

}

if($checkSize3==0) {
echo "There is an error in your third file. <br/>";

}

?>

I added a poster just for file naming purposes only.

Edited by veedeoo: more info. added.

Votes + Comments
Thanks for the script, it's very simple for me to understand! Thanks!
1

simple explantion on what is going in the server once the upload.php is submitted.

  1. PHP will save the files in the tmp directory. This is above the public directory e.g. public_html or htdocs.

  2. Once the files has been uploaded successfuly, the process.php will then execute the

    move_uploaded_file($_FILES['ufile']['tmp_name'][0], $item1);

coding convention of move_uploaded_file

 bool move_uploaded_file ( string $filename , string $destination )

**layman's terms **

MoveThisUploadedFile('FromTempFileDir/tempName','ToUploadsDirectory/AsNewFilename.WithTheExtensionOfTheFileAsAllowed').    

Edited by veedeoo: more info. added.

Votes + Comments
Thanks for the explanation, it was very helpful!
0

@veedeoo

Thanks for the reply! Thanks for the example and explanation! I know you always put your heart into these codes that you posted. I will test it out. I'll let you know how it goes. I also have to create an array on my preivous example. Thanks!

2

@veedeoo

So for replying so late. I had something to do the past few days. So the past hour I finally got a chance to test out your script and it work! It was very simple for me to understand it and also your explanation on how the

move_uploaded_file

function works. I appreciate your help again! Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.