Why won't my PHP file uploader script work?

Dan

New member
It is uploadFile.php for my website.

It indicates that its uploading, but then it does nothing after that. its supposed to tell me whether it worked out or not, and then give a link to the file to view.

This is the code:

<?php
//make sure the user clicked the submit button

if(isset($_POST['submit'])) {
$target = "uploads/";
$file_name = $_FILES['file']['name'];
$tmp_dir = $_FILES['file']['tmp_name'];
echo $_FILES['file']['size'];
// a Try/Catch statement is used to ensure the user uploaded a photo and nothing else. This makes it more secure.
try
{
// a regular expression is used to make sure it ends in a certain file name extension, so that its a picture
if(!preg_match('/(gif|jpe?g|png)$/i', $file_name) ||
//the dollar sign just before the forward slash means it has to END that way, or the user could uploaded a file like gif.txt
!preg_match('/^(image)/', $_FILES['file']['type']) ||
//the above is a reg exp to help ensure that the type of file is IMAGE, and its at the beginning. ( example - file type: Image/Jpeg)
$_FILES['file']['size'] > 300000)
//the file must be smaller than 300 K

{
throw new Exception("It needs to be an actual picture, nothing else will be accepted.");
exit;
}

move_uploaded_file($tmp_dir, $target . $file_name);
$status = true;
// if this status turns out true the user will get a confirmation and link to view the photo
}

catch(Exception $e)
{
echo $e->getMessage();
}

}
$status = "false";
?>

<!DOCTYPE html>

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset-utf-8">
<title>untitled</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST"><!-- if its not multipart/form-data upload won't work -->
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<label for="file">Choose a file to upload:</label> <input id="file" name="file" type="file">
<input type="submit" value="Upload File" name="Submit" />
</form>
<?php

if(isset($status)) {
$path = $target . $file_name;
echo "Congratulations. <a href=\"$path\">View your image.</a>";
}
?>
</body>
</html>
 
Back
Top