0

I am using an Apache Server which works in localhost. I don't know php so I tried the code from w3schools site but I cannot make it work.

Here is the HTML code :

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

The PHP code :

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
echo $target_file;
$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;
    }
}
?>

The echo command in the beginning of the php code, only returns "uploads/" so I guess the $_FILES does not work properly.

EDIT2

I have reinstalled Apache with XAMPP this time and I have no more problem with the code(it outputs "File is an image") but there is still no file uploaded in my "uploads" folder. My "uploads" folder is in the same folder than my web page in localhost.

I am using the default php.ini (file_uploads = on). I tried different files of different size from different directories but no success so far.

oro777
  • 1,110
  • 1
  • 14
  • 29
  • 1
    Did u created `uploads` folder in the same directory you are working ? – Sree KS Jul 13 '16 at 06:31
  • So whats your problem? – Passionate Coder Jul 13 '16 at 06:37
  • I have edited my last sentence : The echo command in the beginning of the php code, only returns "uploads/" so I guess the $_FILES does not work properly. – oro777 Jul 13 '16 at 07:09
  • To debug, please run `var_dump($_FILES)` instead of `echo $target_file`. You should also give more details than "does not work" if you want more solutions than "fix your code". – Álvaro González Jul 13 '16 at 07:12
  • Sorry I meant that the filename should display but it is empty. I tried your debug code and it returns array(0) { } – oro777 Jul 13 '16 at 07:19
  • [enter image description here](https://i.stack.imgur.com/GA6KL.png)My name is Khalis and i am high school students, i just dont know what to do because i cant solve my problem ... so i went here to ask you several questions.. so as i said i am high school students and my course is ICT im stucking in making upload page and i really hope you can solve it ... well im from malaysian i hope you understand my code... i just want you to check mine code that i did ... i used dreamweaver to make system...[](https://i.stack.imgur.com/Xtb4U.png) – MUHAMMAD KHALIS FIRDAUS B SHAS Apr 04 '19 at 05:34

2 Answers2

2

Edited

This should work fine. As for the content-length warning, you will need to edit your php.ini file.
php.ini
Edit these 2 lines.
post_max_size = 200M
upload_max_filesize = 200M

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">
</form>
</body>
</html>

upload.php

<?php
if (!empty($_FILES) && isset($_FILES['fileToUpload'])) {
    switch ($_FILES['fileToUpload']["error"]) {
        case UPLOAD_ERR_OK:
            $target = "upload/";
            $target = $target . basename($_FILES['fileToUpload']['name']);

            if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target)) {
                $status = "The file " . basename($_FILES['fileToUpload']['name']) . " has been uploaded";
                $imageFileType = pathinfo($target, PATHINFO_EXTENSION);
                $check = getimagesize($target);
                if ($check !== false) {
                    echo "File is an image - " . $check["mime"] . ".<br>";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.<br>";
                    $uploadOk = 0;
                }

            } else {
                $status = "Sorry, there was a problem uploading your file.";
            }
            break;

    }

    echo "Status: {$status}<br/>\n";

}
DunnoHowToCode
  • 531
  • 1
  • 4
  • 14
  • I tried to change the target directory to c:/uploads but nothing uploads. The console of my browser is empty and as I wrote the echo is empty for the filename. – oro777 Jul 13 '16 at 07:12
  • @oro777 Give me a while, I try to get my side to work first before editing my answer – DunnoHowToCode Jul 13 '16 at 07:16
  • @oro777 Please give these changes a try and update me on the outcome or any errors. – DunnoHowToCode Jul 13 '16 at 07:48
  • Thanks for the update but I got the following error in my Chrome console : Failed to load resource: the server responded with a status of 500 (Internal Server Error) – oro777 Jul 13 '16 at 07:56
  • @oro777 http://stackoverflow.com/a/19180823/6385066 "The 500 code would normally indicate an error on the server, not anything with your code." You should check the logs. – DunnoHowToCode Jul 13 '16 at 08:00
  • Yes I think the code is correct too but in the Apache log is not updated, only the access.log is. (GET and POST command information but nothing about the error) – oro777 Jul 13 '16 at 08:16
  • After reinstalling Apache with XAMPP, I managed to make your code works ! Thank you for your help. – oro777 Jul 15 '16 at 06:44
0

You code works fine. I just checked it

uploads/untitled.pngFile is an image - image/png.

So you probably missing something else.

Provide list of files you are using and their full code

Maksym Semenykhin
  • 1,924
  • 15
  • 26