0

I am an absolute beginner, and this code is actually much too difficult for me, but I would love the result, so I keep trying ...

Goal: let users add images to my folder called /public_html/uploads/ from this small page https://stegemueller.dk/upload-image/index.php (I first try to make it work, and then comes the design later on):

Actual results: None ... but the url responds that the upload was successful: "https://stegemueller.dk/upload-image/index.php?uploadsuccess" This header can be found in line 22.

Error messages: The folder is not being populated and the error log says for example this:

PHP Warning: move_uploaded_file(/public_html/uploads/ 6322e3df84ff59.44735736 . test.jpg): failed to open stream: No such file or directory in /home/stegemue/public_html/upload-image/upload.php on line 21

I have tried this: Normally my images are called e.g. "Andersen, Hanne.jpg". I thought the issue might be the comma in the filename. I tried to rename an image and called it test.jpg. As you can see from the error message, that did not help.

I guess the problem is the white-spaces in the filename that PHP tries to place in the folder, but I don't know how to get rid of them.

My starting point was this video: https://www.youtube.com/watch?v=JaRq73y5MJk and I have only added ( and destroyed :-) ) line 13, saying: $imgtype = substr($fileActualExt, strpos($fileActualExt, ".") + 1);

I hope someone can explain to me which silly errors I make.

My code is here:

<?php
if(isset($_POST["submit"])) {

$fileName =  $_FILES["file"]["name"];
$fileTmpName =  $_FILES["file"]["tmp_name"];
$fileSize =  $_FILES["file"]["size"];
$fileError =  $_FILES["file"]["error"];
$fileType =  $_FILES["file"]["type"];

$fileExt = explode(',',  $fileName);
$fileActualExt = strtolower(end($fileExt));

$imgtype = substr($fileActualExt, strpos($fileActualExt, ".") + 1);
$allowed = array("jpg", "jpeg", "png", "pdf", "tmp");

if (in_array ($imgtype, $allowed)) {
    if ($fileError === 0) {
        if ($fileSize < 1000000) {
            $fileNameNew = uniqid( ' ', true) . " . " .$fileActualExt;
            $fileDestination = "/public_html/uploads/".$fileNameNew;
            move_uploaded_file($fileTmpName, $fileDestination);
            header("Location: index.php?uploadsuccess");
        } else {
            echo "Your file is too big! Din fil er for stor!";
        }

    } else {
        echo "There was an error uploading your file! Der skete en fejl, da du prøvede at uploade filen!";
    }
} else {
        echo "You cannot upload files of this type. Denne type filer kan ikke uploades!";
    }
}

Is it normal, that I do not get notified when there is an answer to my question? I do want to be polite and answer/comment as fast as possible.

Hi mhaendler Thank for your reply!

I try to mark my code but I can't make it work. I mark all lines, then press CTRL K, but that is not accepted.

I now get this:

https://stegemueller.dk/upload-image/index.php?uploadsuccess

PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpf60x1G' to '/public_html/uploads/63232299eadf46.77449704 . test.jpg' in /home/stegemue/public_html/upload-image/upload.php on line 21

code looks like this: 

I must do something silly wrong with those white spaces, but I can't see them:

if (in_array ($imgtype, $allowed)) { if ($fileError === 0) { if ($fileSize < 1000000) { $fileNameNew = uniqid('', true) . " . ". $fileActualExt; $fileDestination = "/public_html/uploads/".$fileNameNew; move_uploaded_file($fileTmpName, $fileDestination); header("Location: index.php?uploadsuccess");

3 Answers3

0

I thought the issue might be the comma in the filename

Close i see a few error's here.

$fileNameNew = uniqid( ' ', true) . " . " .$fileActualExt;

You generate a new filename with quite a few whitespaces. Remove the whitespaces from your filename :). It's just quick guess, but i think that where you get the error from.

PHP Warning: move_uploaded_file(/public_html/uploads/ 6322e3df84ff59.44735736 . test.jpg):

the error shows that your filename contains multiple whitespaces.

In the video you watched, "uniqid" is without an whitespace in it.

uniqid('', true)

//EDIT Sorry made a silly typo in uniqid

TylerH
  • 20,799
  • 66
  • 75
  • 101
mhaendler
  • 124
  • 5
  • Oh of course, I should have seen the extra white space. I have removed it now. So I have this code: "$fileNameNew = uniquid('', true) . " . " .$fileActualExt;" That makes two things happen: 1) The header (index.php?uploadsuccess) does not show up any more. 2) The file is not uploaded - and the errorlog now returns: PHP Fatal error: Uncaught Error: Call to undefined function uniquid() in /home/stegemue/public_html/upload-image/upload.php:19 I have also adjusted the permissions on the folder called /public_html/uploads to "777" Best regards, – Stegemüller Sep 15 '22 at 10:45
  • Let's examine the next error: >Error: Call to undefined function uniquid() in Sorry i made a typo in my previous post, after fixing that.. how are things going now? – mhaendler Sep 15 '22 at 11:12
  • I make an answer because this site keeps telling me my comment is to long. Sorry for the delay. Hanne – Stegemüller Sep 15 '22 at 13:08
0

Try for the first time Change uniqid(' ', true) to uniqid('', true).

If it is still not resolved, you can check the directory permission settings of the file to be created and whether there are relevant permissions.

wei
  • 11
  • 2
  • Thank you! I have 1) removed the white space 2) Adjusted permissions for the folder: "/public_html/uploads" to 777. Unfortunately it did not solve the problem. Beste regards. – Stegemüller Sep 15 '22 at 10:48
  • @Stegemüller About `PHP Warning: move_ uploaded_ File () disable to move` error. I wonder if it can be solved: [PHP Warning: move_uploaded_file() unable to move](https://stackoverflow.com/questions/13723174/php-warning-move-uploaded-file-unable-to-move) – wei Sep 16 '22 at 07:31
  • Thank you very much. I will study that solution as well. Best regards, – Stegemüller Sep 17 '22 at 08:08
0

The solution is here:

My friend wrote this:

I had to change the code a bit. Think maybe it wanted a relative path instead of a fixed path. That is, the path to the upload directory is defined with ../uploads/. The two dots mean that you go one folder back from where the file is located. In this case, it corresponds to going back to public_html.

Originally I had:

$fileDestination = "/public_html/uploads/".$fileNameNew;

It should be this instead:

move_uploaded_file($tmp_name, "$uploads_dir/$name");

This means that lines 16 - 34 should look like this:

if (in_array ($imgtype, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 1000000) {
                $uploads_dir = '../uploads';
                $tmp_name = $_FILES["file"]["tmp_name"];
                $name = basename($_FILES["file"]["name"]);
                move_uploaded_file($tmp_name, "$uploads_dir/$name");
                header("Location: index.php?uploadsuccess");
            } else {
                echo "Din fil er for stor!";
            }

        } else {
            echo "Der skete en fejl, da du prøvede at uploade filen! Prøv evt. igen";
        }
    } else {
            echo "Denne type filer må ikke uploades!";
        }
}