HTML File Upload Issue using PHP

Status
Not open for further replies.

joe904

Estimable
Apr 11, 2015
11
0
4,560
Hey guys,

I am trying to implement a simple upload form and I am having some issues with the functionality. I found a simple form at W3Schools.com, but when I run it, the file never ends up in the folder that I set-up. I have 2 separate web pages and a "uploads" folder to store the uploaded files . I have already searched online for solutions, but with no luck. My PHP is working fine with no issues. I tried changing the option in my php.ini , "file_uploads" to On, that had little affect. Please any help is appreciated.

Software - Win2012 SE R2, phpmyadmin, IIS8, PHP 7.0.7

First Page (index.html)

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

Second Page (upload.php)

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
 
Solution
Yes it could be the code itself. As for security settings I would expect that some error code or warning would be offered if the necessary rights were not available.

I did find your code verbatim on another website (not W3Schools.com). No way to know if that code is correct. Could have an error that has just propagated through various copies.

Put some test lines in your code to display the values of the variables so you can track them. See where the values vary from what you expect/assign and perhaps get changed later on.

Write a small piece of code with actual pathnames, directory, and file names to force a test file to upload. See if that code generates any error messages or other responses.
With respect to " file never ends up in the folder that I set-up" where does the file end up? Path? Or does the file just end up nowhere?

If the file never ends up where intended then the value of $uploadOk may be staying at or being reset to 0 (zero) for some reason.

Add a couple of test lines to show what the value of $uploadOk is as code executes.
 

joe904

Estimable
Apr 11, 2015
11
0
4,560
Sorry I guess I should have clarified better. When I upload a file, it never ends up in the folder I set-up or anywhere on the server itself. When I run the application it says "File is an image - image/gif." which is good saying that $uploadOK = 1, was successful, but the file never ends up in the "uploads" folder. The "uploads" folder is in the same directory as the "index.html" so could it be a problem with the code that specifies where to save the uploaded file, or maybe something with the security settings of the folder itself. I am open to any and all suggestions.
 
Yes it could be the code itself. As for security settings I would expect that some error code or warning would be offered if the necessary rights were not available.

I did find your code verbatim on another website (not W3Schools.com). No way to know if that code is correct. Could have an error that has just propagated through various copies.

Put some test lines in your code to display the values of the variables so you can track them. See where the values vary from what you expect/assign and perhaps get changed later on.

Write a small piece of code with actual pathnames, directory, and file names to force a test file to upload. See if that code generates any error messages or other responses.
 
Solution
Status
Not open for further replies.

TRENDING THREADS