Error in Writing to Image file from PHP

前端 未结 4 1266
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 06:23

I am attempting to write to an image file from a blob.

 if($_POST[\'logoFilename\'] != \'undefined\'){
  $logoFile = fopen($_POST[\'logoFilename\'], \'w\') o         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 07:07

    If it's a file upload control, $_POST won't contain the information. You're looking for handling file uploads with $_FILES. (And more specifically, move_uploaded_file)

    Given the new update, try the following:

      // 
      // Export a image blob to a file using either the specific image name
      // @blob     : The actual image blob
      // @fileName : Can be the explicit name (with an extension) or this can be
      //             just a generic file name and the extension (based on data
      //             type) will be appended automatically. This can also include
      //             path information.
      // Exmaples:
      //   storeBlob('data:image/png;base64,...', 'myimage');      ::  saves as myimage.png
      //   storeBlob('data:image/jpg;base64,...', 'img/new.jpg');  ::  saves as img/new.jpg
      function storeBlob($blob, $fileName)
      {
        $blobRE = '/^data:((\w+)\/(\w+));base64,(.*)$/';
        if (preg_match($blobRE, $blob, $m))
        {
          $imageName = (preg_match('/\.\w{2,4}$/', $fileName) ? $fileName : sprintf('%s.%s', $fileName, $m[3]));
    
          return file_put_contents($imageName,base64_decode($m[4]));
        }
        return false; // error
      }
    

提交回复
热议问题