I'm currently attempting to write an image to a folder on my webserver, something is being written, but it is not an image. My firedebugger is also giving me an error
我正在尝试将图像写入我的网络服务器上的文件夹,正在编写一些内容,但它不是图像。我的firedebugger也给了我一个错误
PHP Warning: imagecreatefromstring() : Data is not in a recognized form
The file that is written has the correct name, but is not openable by text or image programs. When angularJS uploads an image the $_POST data that php is attempting to retrieve begins with this.
写入的文件具有正确的名称,但不能通过文本或图像程序打开。当angularJS上传图像时,php试图检索的$ _POST数据从此开始。
data:image/jpeg;base64,/9j/
PHP
PHP
<?php
ini_set("upload_max_filesize", "100M"); // to avoid anyone can send a gigabyte of file
ini_set("post_max_size", "101M"); // and break down the server...
define("MAX_IMGSIZE", 500000);
define("UPLOAD_DIR", (realpath("../uploads")).DIRECTORY_SEPARATOR);
require_once("functions.php");
// define("XHR", isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ? true : false : false);
// if(XHR)
// {
// echo "yes";
// }
$errors = [];
if(!empty($_POST["img"]) && !empty($_POST["imgsize"]) && !empty($_POST["imgname"]) && !empty($_POST["filter"]))
{
// __________ ____ __ ___
// / ____/ __ \/ __ \/ |/ /
// / /_ / / / / /_/ / /|_/ /
// / __/ / /_/ / _, _/ / / /
// /_/ \____/_/ |_/_/ /_/
//
// or extract($_POST);
$img = $_POST["img"];
$imgsize = intval($_POST["imgsize"]);
$imgname = htmlentities(trim($_POST["imgname"]));
$imgname = array_shift(explode('.', $imgname));
$filter = htmlentities($_POST["filter"]);
if(strlen($imgname) == 0) // if image uploaded with empty name
$errors[] = ["Error 54", "Image filename is empty"];
if(check_base64_image($img))
$errors[] = "Uploaded image is not valid!";
if($imgsize >= MAX_IMGSIZE)
$errors[] = sprintf("Uploaded image is too large! Maximum wieght is : %s Kb", MAX_IMGSIZE/1000);
if(is_dir(UPLOAD_DIR)) // if upload directory exists
{
if(file_put_contents(UPLOAD_DIR."${imgname}_".time(), base64_decode($img)))
{
// ____ ____ ______
// / __ \/ __ \/ ____/
// / / / / /_/ / /
// / /_/ / _, _/ /___
// \____/_/ |_|\____/
//
// ALL SOUNDS GOOD --> ORC BEGIN
if(exec("binary path... arg0 arg1", $o))
{
// rest of process
}else
$errors[] = ["Error 12", "Binary failed"];
}else
$errors[] = ["Error 32", "Cannot write image file"];
}else
$errors[] = ["Error 42", "Upload dir does not exists"];
}else
$errors[] = "Filter and Image are required, please complete the form!";
// __________ ____ ____ ____ _____
// / ____/ __ \/ __ \/ __ \/ __ \/ ___/
// / __/ / /_/ / /_/ / / / / /_/ /\__ \
// / /___/ _, _/ _, _/ /_/ / _, _/___/ /
// /_____/_/ |_/_/ |_|\____/_/ |_|/____/
//
if(count($errors)) // check first system error, and shift them
{
foreach($errors as $ie => $e)
{
if(is_array($e)) // system error, do not show in front for security
{
error_log($e[0]);
unset($errors[$ie]);
}
}
if(count($errors)) // if staying errors, inform the user
exit(json_encode($errors));
}
?>
<!--
if(isset($_FILES['fileToUpload'])){
$errors= array();
$file_name = $_FILES['fileToUpload']['name'];
$file_size =$_FILES['fileToUpload']['size'];
$file_tmp =$_FILES['fileToUpload']['tmp_name'];
$file_type=$_FILES['fileToUpload']['type'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== false){
$errors[]="image extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size cannot exceed 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../uploads/".$file_name);
echo " uploaded file: " . "images/" . $file_name;
}else{
print_r($errors);
}
}
/*else{
$errors= array();
$errors[]="No image found";
print_r($errors);
}*/
-->
<!-- function printArray($array){
foreach($array as $key => $value){
echo "$key => $value";
if(is_array($value)){
printArray($value);
}
}
} -->
1 个解决方案
#1
1
Found that the header information from the angularJS was the problem. To fix this I removed
发现angularJS的头信息是问题所在。为了解决这个问题,我删除
data:image/jpeg;base64,
before the actual base64 string that is the image in the post data. To fix this I added a function that does the following.
在实际的base64字符串之前,即post数据中的图像。为了解决这个问题,我添加了一个执行以下操作的功能
function headerRemove($img){
$pos = strpos($img, ",",11);
$img = substr($img, $pos, strlen($img));
return img;
}
In angularJS I found that a "," was used before the base64 string of the actual image began. The index 11 position was used because the proper place to start looking for the 2nd "," depends on whether or not the image type is a png or jpeg.
在angularJS中,我发现在实际图像的base64字符串开始之前使用了“,”。使用索引11位置是因为开始寻找第二个“,”的适当位置取决于图像类型是png还是jpeg。
#1
1
Found that the header information from the angularJS was the problem. To fix this I removed
发现angularJS的头信息是问题所在。为了解决这个问题,我删除
data:image/jpeg;base64,
before the actual base64 string that is the image in the post data. To fix this I added a function that does the following.
在实际的base64字符串之前,即post数据中的图像。为了解决这个问题,我添加了一个执行以下操作的功能
function headerRemove($img){
$pos = strpos($img, ",",11);
$img = substr($img, $pos, strlen($img));
return img;
}
In angularJS I found that a "," was used before the base64 string of the actual image began. The index 11 position was used because the proper place to start looking for the 2nd "," depends on whether or not the image type is a png or jpeg.
在angularJS中,我发现在实际图像的base64字符串开始之前使用了“,”。使用索引11位置是因为开始寻找第二个“,”的适当位置取决于图像类型是png还是jpeg。