Php upload image to directory

后端 未结 6 418
野的像风
野的像风 2021-01-13 16:25

Ive been experimenting with the upload capability of php, I have been looking at this tutorial on w3school.

http://www.w3schools.com/php/php_file_upload.asp
         


        
6条回答
  •  春和景丽
    2021-01-13 17:15

    The problem of your script is that you are probably trying to upload a file higher that your limit is! You specified a really low max filesize! Print out your $_FILES var to get information about what's wrong ;)

    However, in order to move the file to your folder you still need to use move_uploaded_file:

    $allow = array("jpg", "jpeg", "gif", "png");
    
    $todir = 'uploads/';
    
    if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
    {
        $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file
    
        if ( in_array( end($info), $allow) ) // is this file allowed
        {
            if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
            {
                // the file has been moved correctly
            }
        }
        else
        {
            // error this file ext is not allowed
        }
    }
    

提交回复
热议问题