-1

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.

3
Contributors
15
Replies
16
Views
6 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 1
    diafol 3,669   6 Years Ago

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

  • 1
    diafol 3,669   6 Years Ago

    It suggests something missing like a ; or an extra } Try this [CODE]<?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: " . … Read More

0

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.

0

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

0

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.

0

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.

0

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

0

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

1

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";
}
?>
Votes + Comments
Code seem better now :)
0

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

0

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

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

0

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.

Edited by Ezzaral: Snipped url. Please restrict personal links to your signature.

0

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

0

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.

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.