PHP image upload problems

穿精又带淫゛_ 提交于 2019-12-25 17:19:10

问题


I'm trying to create a website were pictures can be uploaded and saved into a folder in the server. However, the code does not go past the second if. Is there something I'm missing? Thanks in advance.

The PHP:

if (isset($_FILES["file"]["name"])){
        if($_FILES['image']['error']==0){
            $file = $_FILES['image']['tmp_name'];
            $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
            $image_name= addslashes($_FILES['image']['name']);

            $newname = $erabiltzaile.".png"; 

            $target = 'irudiak/profile/users/'.$newname;
            move_uploaded_file( $_FILES['image']['name'], $target);

        }
    }else{
        $image=null;
        $image_name="";

    }

The HTML:

<div id='igoera'>
<input name='image' id ='image' type='file' accept='image/png' onchange='loadFile(event)' /><br /><br />
<img width='300' id='preview'/><br>
</div>

回答1:


Looks like you've missed input file name in your condition:

if (isset($_FILES["image"]["name"])){



回答2:


In the manual there's a section on file uploads http://php.net/manual/en/features.file-upload.php which shows the values for the error variable in the $_FILES array.

A successful upload should result in the error value being UPLOAD_ERR_OK.

So change the second if condition to:

if($_FILES['image']['error']=='UPLOAD_ERR_OK'){


来源:https://stackoverflow.com/questions/34590212/php-image-upload-problems

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