doupinge9055 2016-05-07 23:50 采纳率: 100%
浏览 182

如何使用php将图像上传到服务器上的文件夹目录中

I am trying to upload images onto server.

On the Server the folder name:{photo}

I check the permissions on the folder and it currently on 0755.

When I run my php code, I get this error code:

"Error uploading file - check destination is writeable."

The post that was similar to my issues is this: How to upload photo to my hosting server folder directory

but I already have these functions in my code:

Here my php code:

<?php

$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filesize = $_FILES["file_img"]["size"];
$fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
$filepath = "../photo/".$filename;
$filepath_thumb = "../photo/thumb/".$filename;




if($_POST['btn_upload'])
{
    $sPhotoFileName = $filename;
    $nPhotoSize = $filesize;
    $sTempFileName = $filetmp;
    chmod($filepath_thumb,0755);
    chmod($filepath,0755);

if(file_exists('photo/' . $_FILES['file_img']['name'])){
    die('File with that name already exists.');
}else{

if ($sPhotoFileName) // file uploaded
{   $aFileNameParts = explode(".", $sPhotoFileName);
    $sFileExtension = end($aFileNameParts); // part behind last dot
    if ($sFileExtension != "jpg"
        && $sFileExtension != "png"
        && $sFileExtension != "gif")
    {   die ("Choose a JPG for the photo");
    }
}

if($_FILES['file_img']['error'] > 0){
    die('An error ocurred when uploading.');
}

    if ($nPhotoSize == 0)
    {   die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 300K, using the button.");
    }
    if ($nPhotoSize > 30240000000)
    {   die ("Sorry.
The file $sPhotoFileName is larger than 300K.
Advice: reduce the photo using a drawing tool.");
    }
    // read photo

    $oTempFile = fopen($sTempFileName, "r");
    $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
    // Try to read image
    $nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
    $oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
    error_reporting($nOldErrorReporting);
    if (!$oSourceImage) // error, image is not a valid jpg
    { die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
    }
}
 $nWidth = imagesx($oSourceImage); // get original source image width
        $nHeight = imagesy($oSourceImage); // and height
        // create small thumbnail
        $nDestinationWidth = 80;
        $nDestinationHeight = 60;
    //$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
        $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
    /*$oResult = imagecopyresampled(
        $oDestinationImage, $oSourceImage,
        0, 0, 0, 0,
        $nDestinationWidth, $nDestinationHeight,
        $nWidth, $nHeight); // resize the image
    */
        imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
        ob_start(); // Start capturing stdout.
        imageJPEG($oDestinationImage); // As though output to browser.
        $sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

    // attempt insert query execution
    $sql = "INSERT INTO UploadImg (img_name, img_path, img_type) VALUES ('$sPhotoFileName', '$filepath', '$filetype')";
        if(mysqli_query($link, $sql)){
                    echo "Records added successfully.";
                } else{
                    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                }


    if(!move_uploaded_file($_FILES["file_img"]["tmp_name"],"../photo/".$_FILES["file_img"]["name"])){
           die('Error uploading file - check destination is writeable.');
       echo "Error Code: " .$_FILES["file_img"]["name"] . "<br>";
    }else{



    $sBinaryThumbnail = addslashes($sBinaryThumbnail);
    $oDatabase = $link;
    mysqli_select_db("upload", $oDatabase);
    $sQuery = "insert into Uploadimg (thumbnail) VALUES ('$sBinaryThumbnail')";
    echo $sQuery;
    mysqli_query($sQuery, $oDatabase);
    die('File uploaded successfully.');
    mysqli_close($link);

    }
}
    ?>

Now I read an article say that even if your folder permission setup up to do all three read, write, and executed on all three level. the code still will not be able to read it depending on the settings on the server.

So I am confused and looking for clarification. Please assist me?

  • 写回答

4条回答

  • duandeng1824 2016-05-07 23:57
    关注

    You can upload the image by binary data encoded and save the file with the image format on the server.

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题