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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (22)

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

我的uploadfolder在这里:/ storage / www / messenger / data / profilepictures

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

这是我的代码

变化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>

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

?>

提前致谢。

提问于
用户回答回答于

尝试使用$_SERVER['DOCUMENT_ROOT']superglobal。

在它前:

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

在它之后:

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

扫码关注云+社区

领取腾讯云代金券