在图片上传时设置chmod 777

时间:2014-09-18 11:17:00

标签: php html file-permissions

如何在此代码中设置chmod 777?我在将图片上传到服务器时遇到问题,您可以帮忙解决这个问题吗?

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$newfilename = time().end($temp).$_FILES["file"]["name"];

if(!empty($_FILES["file"]["name"])){
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && in_array($extension, $allowedExts)) 
    {


        $sql = "INSERT INTO `doctor-info` VALUES ('', '$licno', '$txtlname', '$fname', '$mname', '$address','$email' ,'$mobile', '$specialization','$twopecial','$threespecial','$fourspecial', '$newfilename')";
        mysql_query($sql);

        move_uploaded_file($_FILES["file"]["tmp_name"],"images/upload/" . $newfilename);

        echo "<script> alert('data save!'); </script>";
        $get_id =  mysql_insert_id();
        $sql2 = mysql_query("SELECT id FROM `doctor-info` WHERE id='$get_id'");
        $row=mysql_fetch_array($sql2);
        $id = $row['id'];

        echo "<script> window.location = 'doctor.php?action=edit&id=$id'</script>";
    }
    else{

        echo "<script> alert('Not an images!'); </script>";

    }
}

3 个答案:

答案 0 :(得分:3)

首先设置上传文件夹的权限

if(!is_dir('/path/to/project/folder/images/upload/')){
     mkdir('/path/to/project/folder/images/upload/');
}
chmod('/path/to/project/folder/images/upload/',0777);

然后上传文件

move_uploaded_file($_FILES["file"]["tmp_name"],"images/upload/" . $newfilename);

您的图片文件无需呈现

答案 1 :(得分:0)

您可以使用以下代码设置文件的chmod:

move_uploaded_file($_FILES["file"]["tmp_name"],"images/upload/" . $newfilename);
chmod("/var/www/project/images/upload/" .  $newfilename) , 0777);  // specify the absolute path to file 

答案 2 :(得分:-3)

您无需为图像设置执行标志。 666应该绰绰有余。只需在要上传的文件夹上设置这些权限即可。