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");