我有文件上传脚本,我有一个if语句,检查文件是否不存在,它会创建一个并将文件上传到它,如果它存在,它会将文件上传到它。
<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');
if (isset($_POST['do']) and $_POST['do'] == 'upload') {
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$date = "[".date("d-m-Y_H-i")."]";
$temp = explode(".", $filename);
$new_filename = $temp[0] . ""/*you can put any thing that you want before the [] in the 2 singels*/ . $date .'.' . end($temp);
$f_folder = "uploads" . DIRECTORY_SEPARATOR .$_POST['user'] . DIRECTORY_SEPARATOR . $new_filename;
$username = $_POST['user'];
//echo $username;
if (!file_exists("uploads". DIRECTORY_SEPARATOR .$_POST['user'])){
mkdir("uploads" . DIRECTORY_SEPARATOR . $_POST['user'],0777);
if (empty($filename)) {
echo "choose file please";
}else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
}
}else if (empty($filename)) {
echo "choose file please";
}else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
};
};
echo "<hr>";
echo "
<form method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
Username : <input type='text' name='user' />
<br>
<input type='submit' name='do' value='upload' />
</form>";
?>
它工作正常但是当我上传照片时,我无法打开它。它给了我以下错误:
Windows照片查看器无法打开此图片,因为您没有 正确的访问文件位置的权限
-i已更新代码
答案 0 :(得分:2)
使用php的chmod()功能在图片上传后设置权限。
<?php
if (!file_exists("uploads". DIRECTORY_SEPARATOR .$_POST['user'])){
$oldmask = umask(0);
mkdir("uploads" . DIRECTORY_SEPARATOR . $_POST['user'],777);
umask($oldmask);
if (empty($filename)) {
echo "choose file please";
}else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
}
}else if (empty($filename)) {
echo "choose file please";
}else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
}
chmod($f_folder, 777); // add this line after image upload