-1

I'm trying to make an easy image upload for my webchat application. I've switched from Windows to Linux (xampp to lampp) and now my fileupload script isn't working any more... I tried to change the directory owner with chown and also to change the read/write rights but nothing worked for me, so now I'm trying my best on stackoverflow.

My uploadfolder is here: /storage/www/messenger/data/profilepictures

and my script is here: /storage/www/messenger/functions/upload-img.php

here is my code

change-image.html

<form action="/messenger/functions/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="datei"><br>
<input type="submit" value="Hochladen">
</form>

upload-img.php

<?php
$upload_folder = '/messenger/data/profilepictures/'; //Upload directory

$filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);

$extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));


//checking of the file extention

$allowed_extensions = array('png', 'jpg', 'jpeg', 'gif');

if(!in_array($extension, $allowed_extensions)) {

 die("Only png, jpg, jpeg and gif-files are allowed");

}

//checking the file size

$max_size = 52428800; //500 MB

if($_FILES['datei']['size'] > $max_size) {

 die("500MB is the max file size");

}

//checking if the file is ok

if(function_exists('exif_imagetype')) { //Die exif_imagetype-Funktion erfordert die exif-Erweiterung auf dem Server

 $allowed_types = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);

 $detected_type = exif_imagetype($_FILES['datei']['tmp_name']);

 if(!in_array($detected_type, $allowed_types)) {

 die("you are not allowed to upload other files than pictures");

 }

}

//path to upload

$new_path = $upload_folder.$filename.'.'.$extension;

//Neuer Dateiname falls die Datei bereits existiert

if(file_exists($new_path)) { //Falls Datei existiert, hänge eine Zahl an den Dateinamen

 $id = 1;



do {



$new_path = $upload_folder.$filename.'_'.$id.'.'.$extension;

 $id++;

 } while(file_exists($new_path));

}

//everything alright, move file to new pos.

move_uploaded_file($_FILES['datei']['tmp_name'], $new_path);

echo 'Bild erfolgreich hochgeladen: <a href="'.$new_path.'">'.$new_path.'</a>';

?>

thanks in advance.

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Adrian S
  • 1
  • 1

1 Answers1

0

Try using $_SERVER['DOCUMENT_ROOT'] superglobal.

Before:

$upload_folder = '/messenger/data/profilepictures/';

After:

$upload_folder = $_SERVER['DOCUMENT_ROOT'].'/messenger/data/profilepictures/';

Andrii H.
  • 1,682
  • 3
  • 20
  • 40