4

有没有办法将 base64 图像字符串添加到$_FILES数组(上传文件时得到的数组)?基本上,我想从 base64 字符串伪造文件上传。base64 字符串来自电子邮件附件。我不想使用file_get_contents(),因为我想将图像作为二进制图像保存在数据库中。因此,我不需要上传它,但我确实需要它成为$_FILES数组的一部分。

4

2 回答 2

5

Uploading is a way of sending the contents of a file from a client (usually a web browser) to a server. It sounds like you've already got the data you need on the server (in PHP), so you don't need to upload it.

  1. You can use base64_decode() to convert it from base64 to the straight file contents.

  2. If you need it as a file on your server, you can use something like file_put_contents() to save it as a new file.

  3. If you're trying to do something else with the uploaded file in the $_FILE array, please provide more details. Whatever it is, you don't need to actually upload the file.


The $_FILES array doesn't actually contain the contents of the file - it contains a filename where the contents have been saved.

If you must use the $_FILES array (in order to re-use an existing class, for example), you'll need to use file_put_contents() to save a new temporary file, and then manually add an entry the the $_FILES array pointing to that temporary file (Just like modifying any other array). Don't forget to delete your temporary file when you're done - this is normally handled automatically by PHP, but if you create the file manually, you'll have to delete it manually too.

I'd highly recommend refactoring or providing an alternate interface to your class instead though. You shouldn't have to pretend that the file was uploaded just to save it to the database.

于 2013-07-23T06:42:16.300 回答
0

索引.php

// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL

/* Simple */
function previewImage( image, preview, string )
{

    var fileImage   = image.files[0];
    var preview     = document.querySelector( preview );

    var reader      = new FileReader();

    reader.addEventListener( "load", function() {

        preview.style.height    = "100";
        preview.title           = fileImage.name;

        // convert image file to base64 string
        preview.src             = this.result;

        /* --- */

        document.querySelector( string ).value = this.result;

    }, false );

    if ( fileImage )
    {
        reader.readAsDataURL( fileImage );
    }

}

document.querySelector( "#imageID" ).addEventListener( "change", function() {

    previewImage( this, "#imagePreviewID", "#imageStringID" );

} )
/* Simple || */
<form method="post" action="process.php" >

    Title: <input type="text" name="title" /><br />

    File Upload: <input type="file" name="image" id="imageID" /><br />
    Preview: <img src="#" id="imagePreviewID" /><br />
    String base64: <textarea name="imageString" id="imageStringID" rows="10" cols="50" readonly="readonly" ></textarea>

    <hr />

    <input type="submit" name="submit" value="Submit" />

</form>


进程.php

<?php

    $data = array(
        "post"  => $_POST,
        "files" => $_FILES
    );

    echo "<pre>";

    // echo json_encode($data);

    var_dump($data);

?>

<img src="<?= $_POST['imageString']; ?>" />

复制

于 2020-12-10T11:30:48.123 回答