PHP - 将二进制转换为图像并将其存储在服务器上

时间:2015-03-09 17:13:09

标签: php image file

您好我有PHP脚本来检索图像和文件名。我已成功获得图像和文件名。这没有问题。转换为二进制文件后,我想将其保存为服务器上的图像。但我无法保存该图像。这是我的PHP脚本。

<?php

    // Get image string posted from Android App
    $base=$_REQUEST['image'];
    // Get file name posted from Android App
    $filename = $_REQUEST['filename'];
    // Decode Image
    $binary=base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');
    // Images will be saved under 'www/imgupload/uplodedimages' folder
    $file = fopen('uploadedimages/'.$filename, 'wb');
    // Create File
    fwrite($file, $binary);
    fclose($file);
    echo 'Image upload complete, Please check your php file directory\n';

?>

1 个答案:

答案 0 :(得分:1)

这是一个简单的代码,用于在我的网络服务器上存储图像,它不会将图像转换为二进制文件,但它可以将图像存储到服务器上。

您应该创建一个uploads文件夹,并为其提供777等完整权限。

<?php
echo $_FILES['image']['name'] . '<br/>';


//ini_set('upload_max_filesize', '10M');
//ini_set('post_max_size', '10M');
//ini_set('max_input_time', 300);
//ini_set('max_execution_time', 300);


$target_path = "uploads/";

$target_path = $target_path . basename($_FILES['image']['name']);

try {
    //throw exception if can't move the file
    if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
        throw new Exception('Could not move file');
    }

    echo "The file " . basename($_FILES['image']['name']) .
    " has been uploaded";
} catch (Exception $e) {
    die('File did not upload: ' . $e->getMessage());
}
?>

链接到php文件的HTML(供您参考)

<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
        <form enctype="multipart/form-data" action="fileUpload.php" method="POST">            
            Select a file<input name="image" type="file" /><br />
            <input type="submit" value="Upload File" />
        </form>
    </body>
</html>