How to upload image using file_get_contents in php
I am new In Reactjs and php, I have index.js (page in Reactjs/nextjs) and i am sending image data (using multipart form data) And in Php i am trying to upload image but whenever i am trying to upload image then uploaded 开发者_运维百科image showing error "we dont support this format", So please tell me how can i simply upload image using "file_get_contents" method, Here is my code in php (which is uploading incorrect image or 0 byte image), i tried with 2 ways , First way/code is
$data = json_decode(file_get_contents("php://input"), TRUE);
$files=file_get_contents($_FILES["file"]["tmp_name"]);
$image = base64_decode(explode( ',', $files)[1]);
$file_name =$_FILES['file']['name'];
$file_ext = strtolower( end(explode('.',$file_name)));
define('UPLOAD_DIR', 'uploads/');
$file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$file_name = UPLOAD_DIR . uniqid() . time() . '.' . $file_ext;
move_uploaded_file($_FILES["file"]["tmp_name"], $file_name);
Second way is
$file_name =$_FILES['file']['name'];
$file_ext = strtolower( end(explode('.',$file_name)));
define('UPLOAD_DIR', 'uploads/');
$image_parts = explode(";base64,", $image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = UPLOAD_DIR . uniqid() . '.'.$file_ext;
file_put_contents($file, $image_base64);
I have a simple upload class that I edit for each project, test it, hope it helps
class Upload
{
//300 kb
private static $MAX_FILE_SIZE = 300 * 1024;
private static $ALLOWED_FILE_TYPES = ['png', 'jpg', 'jpeg'];
private static $key;
private static $ImageFileType;
public static function uploadTeamLogo($key, $short_name, $relativePath)
{
self::$key = $key;
if (!self::isImage())
apiErrorMessage("Not an image!");
if (!self::fileTypeIsAllowed(basename($_FILES[self::$key]["name"])))
apiErrorMessage("Only png, jpg, jpeg Images are allowed!");
if (self::isSizeTooLarge())
apiErrorMessage("Max file size is 300kb!");
$image_name = $short_name . "_" . generateHash(12) . "." . self::$ImageFileType;
$target_dir = getcwd() . $relativePath . $image_name;
if (self::doUpload($target_dir))
return $image_name;
else
return false;
}
private static function isImage()
{
return getimagesize($_FILES[self::$key]["tmp_name"]) == true;
}
private static function alreadyExist($target_file)
{
return file_exists($target_file) === true;
}
private static function isSizeTooLarge()
{
return $_FILES[self::$key]["size"] > self::$MAX_FILE_SIZE === true;
}
private static function fileTypeIsAllowed($target_file)
{
self::$ImageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
foreach (self::$ALLOWED_FILE_TYPES as $type)
if (self::$ImageFileType == $type)
return true;
return false;
}
private static function doUpload($target_file)
{
return move_uploaded_file($_FILES[self::$key]["tmp_name"], $target_file) == true;
}
}
use like this:
$image_name = Upload::uploadTeamLogo($key, $short_name, $path);
精彩评论