我正在尝试上传文件,但它不工作
Posted
技术标签:
【中文标题】我正在尝试上传文件,但它不工作【英文标题】:i am trying to make a file upload but it is not working 【发布时间】:2019-05-08 14:16:27 【问题描述】:我正在尝试为我的网络聊天应用程序轻松上传图片。我已经从 Windows 切换到 Linux(xampp 到lampp),现在我的文件上传脚本不再工作了......我尝试用 chown 更改目录所有者,并更改读/写权限,但对我没有任何作用,所以现在我在***上尽我所能。
我的上传文件夹在这里:/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>
上传-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】:尝试使用$_SERVER['DOCUMENT_ROOT']
superglobal。
之前:
$upload_folder = '/messenger/data/profilepictures/';
之后:
$upload_folder = $_SERVER['DOCUMENT_ROOT'].'/messenger/data/profilepictures/';
【讨论】:
以上是关于我正在尝试上传文件,但它不工作的主要内容,如果未能解决你的问题,请参考以下文章