douang2297 2015-07-01 07:21
浏览 67

因为您没有权限,无法打开此图片

I have file uploading script and I have an if statement that checks if the file does not exists it will create one and upload file to it and if it exists it will upload the file to it.

<?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>";

?>

It works correctly but when I upload a photo, I can't open it. It gives me the following error:

windows photo viewer can't open this picture because you don't have the correct permissions to access the file location

-i have updated the code

  • 写回答

1条回答 默认 最新

  • drydaenth257216154 2015-07-01 07:26
    关注

    Use chmod() function of php to set permission after image upload.

    <?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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c