我正在尝试上传文件,但无法正常工作

时间:2018-12-06 20:09:57

标签: php ubuntu file-upload xampp lampp

我正在尝试为我的网络聊天应用程序轻松上传图像。我已经从Windows切换到Linux(从xampp切换到lampp),现在我的fileupload脚本不再起作用了。我尝试使用chown更改目录所有者,还尝试更改了读/写权限,但是对我没有用,所以现在我正在尽力在stackoverflow上。

我的上载文件夹在这里:/ storage / www / messenger / data / profilepictures

我的脚本在这里:/storage/www/messenger/functions/upload-img.php

这是我的代码

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>';

?>

提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用$_SERVER['DOCUMENT_ROOT']超全局变量。

之前:

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

之后:

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