首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我正在尝试上传文件,但它不起作用

我正在尝试上传文件,但它不起作用
EN

Stack Overflow用户
提问于 2018-12-07 04:09:57
回答 1查看 79关注 0票数 -1

我正在试着为我的网络聊天应用程序做一个简单的图片上传。我已经从Windows切换到Linux (xampp到lampp),现在我的fileupload脚本不再工作了……我试图用chown更改目录所有者,也想更改读/写权限,但对我来说都不起作用,所以现在我正在尽力使用stackoverflow。

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

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

以下是我的代码

change-image.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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>';

?>

提前谢谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53658937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档