Hi i am wondering could anyone help me understand a file upload code. I am currently working on a website like mediafire, raipidshare and the rest of them. I have taken down codes of all sorts and some i got to work but had errors. I don't understand as i take them down perfect and they still don't work. And as i am making a File Uploading site i need to understand them more. I am newish to coding but am a good quick learner and can do everything but use MySQL or connect to a DB. Any help is useful and a code would be nice too :)
Regards,
-Dyla.

Member Avatar for diafol

Want to post the code you're using and let us know what the error seems to be?

Yeah sure :) I used the code from W3schools as that seems to be the best place to get codes.

This is index.php

<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>

Upload_file.php

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

I copied them directly down to see did they work first. The form works as i chose what i upload then when i press submit the code from Upload_file.php just shows up on the screen. I have used a few others and still they didnt work.

Is it on a server that is correctly configured to serve PHP files?

I dont have it online yet i only have it in wamp server and wamp supports php yes.

Member Avatar for diafol

Check that your directory references are correct. Is upload directory in the same directory as Upload_file.php? If not, that may be your problem. Also, ensure that the directory is writable.

I have a directory just for the 3 upload files. (index.php, upload_file.php and upload folder) And i am still getting the code when i click submit. Test it yourself and see what you get as it could be just me.

Ok got it working but am getting an error. Line 24 of upload_file.php

Member Avatar for diafol

WHat's the error.....

I click submit, the file uploads as i see it bottom left of the screen then i get...

Parse error: syntax error, unexpected '}' in /home/a6181642/public_html/web/Uploads/upload_file.php on line 24

Member Avatar for diafol

It suggests something missing like a ; or an extra }

Try this

<?php
if(($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg") && $_FILES["file"]["size"] < 20000) {
	if($_FILES["file"]["error"] > 0) {
		echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

		if(file_exists("upload/" . $_FILES["file"]["name"])) {
			echo $_FILES["file"]["name"] . " already exists. ";
		} else {
			move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
			echo "Stored in:  upload/" . $_FILES["file"]["name"];
		}
	}
} else {
	echo "Invalid file";
}
?>
commented: Code seem better now :) +1

Hmm still another error.

Upload: Image.jpg
Type: image/jpeg
Size: 184.2890625 Kb
Temp file: /tmp/phpsNHDVh

PHP Error Message

Warning: move_uploaded_file(upload/Image.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/a6181642/public_html/web/Uploads/upload_file.php on line 14


Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpsNHDVh' to 'upload/Image.jpg' in /home/a6181642/public_html/web/Uploads/upload_file.php on line 14


Stored in: upload/Image.jpg

Member Avatar for diafol

OK seems we're getting closer. Seems the directory does not exist.

The directory "Uploads" exists but not "upload".

I have a dir called upload yes. still getting the error though...hmm. I have made a free hosting site if you want to check it out.

Nevermind, Permissions to 777 and it works perfect now :) Thanks for the help :D

Member Avatar for diafol

As I mnetioned previously:

> Check that your directory references are correct. Is upload directory in the same directory as Upload_file.php? If not, that may be your problem. Also, ensure that the directory is writable.

If this is solved, mark it so by the link below.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.