Having issue creating an Upload-File Form
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!
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
pritaeas
Posting Prodigy
9,267 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
@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.
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
@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.
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
@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!
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
That means that $_FILES["pictures"]["error"] is not an array.
pritaeas
Posting Prodigy
9,267 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86
@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.
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
@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!
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
@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!
LastMitch
Industrious Poster
4,132 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
Question Answered as of 9 Months Ago by
pritaeas,
veedeoo
and
DarkMonarch