PHP - Saving Uploaded Image to Server

两盒软妹~` 提交于 2019-12-04 18:33:39

Try the following step before you move your image

if(!is_dir($folder))
{
   echo "Folder does not exist" ;

   if(!mkdir($folder))
   {
      echo "Attempt to create folder failed" ;
   }
}
else if(!is_writable($folder))
{
   echo "Folder nto writeable" ;

   if(!chmod($folder, 0755))
   {
        echo "Attempt to make writeable failed" ;
   }
}
 else {

   if (move_uploaded_file($_FILES['img']['tmp_name'], $folder . $image_path)) {
      echo 'ok!';
   }
   else {
    echo 'fail !';
   }    
}

What do you get ???

Please also remember to do security checks on the file being uploaded. For example, check the file types against a whitelist (jpeg, png, gif, etc)- I'm sure you don't want someone being able to upload a script to your website?

Also for the long term you would be better off manually creating the upload folder and setting the appropriate permissions as oppose to having the PHP script do it for you..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!