Home > OS >  Trying to upload images to a small page (I am absolute beginner)
Trying to upload images to a small page (I am absolute beginner)

Time:09-15

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!";
    }
}

CodePudding user response:

Try for the first time Change uniquid(' ', true) to uniquid('', 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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related