简体   繁体   English

PHP $ _FILES [“ fileToUpload”] [“ tmp_name”]

[英]PHP $_FILES[“fileToUpload”][“tmp_name”]

How can I turn $_FILES["fileToUpload"]["tmp_name"] into a variable to be used in a move_uploaded_file? 如何将$_FILES["fileToUpload"]["tmp_name"]转换为在move_uploaded_file中使用的变量?

This works: 这有效:

  $filename = compress_image(($_FILES["fileToUpload"]["tmp_name"]), $url, 30);

But I am trying to get something like this: 但是我试图得到这样的东西:

  $filename = compress_image($images, $url, 30);

But, when do above it does not work. 但是,当执行上述操作时不起作用。

One alternative I was starting on was: 我开始的另一种选择是:

  file_put_contents($target_file , $image);

In that case, the image was named into directory properly, but the image was always broken. 在这种情况下,该映像已正确命名到目录中,但该映像始终损坏。

To clarify: 澄清:

Need to turn ($_FILES["fileToUpload"]["tmp_name"]) into a variable that is result of 需要将($ _FILES [“ fileToUpload”] [“ tmp_name”])转换为以下结果的变量:

    ob_start(); echo imagejpeg($image,null,30); 
    $image =ob_get_clean(); 
    ob_end_clean();
     $image = addslashes($image); 

I need to use $image to save to directory. 我需要使用$ image保存到目录。 $image has been successfully stored into mysql. $ image已成功存储到mysql中。 I have tried encode, and decode on $image, still no luck. 我已经尝试对$ image进行编码和解码,但是还是没有运气。

Let me explain the problem step-by-step: 让我逐步解释问题:

The mess starts at this line: 混乱始于这一行:

$image = imagecreatefromstring(file_get_contents($_FILES['fileToUpload']['tmp_name']));

Problems: 问题:

  • Never use temp file to process; 切勿使用临时文件进行处理; move the uploaded file to a permanent place by move_uploaded_file() first 首先通过move_uploaded_file()将上传的文件移动到永久位置
  • Use of file_get_contents() is unnecessary; 不需要使用file_get_contents() you can use imagecreatefromjpeg() (and other formats, like GIF, PNG, etc) to replace the imagecreatefromstring(file_get_contents()) things 您可以使用imagecreatefromjpeg() (以及其他格式,例如GIF,PNG等)替换imagecreatefromstring(file_get_contents())内容

Here is your compress_image() function: 这是您的compress_image()函数:

function compress_image($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);
    if ($info['mime'] == 'image/jpeg')  {
        $image = imagecreatefromjpeg($source_url);
    } elseif ($info['mime'] == 'image/gif') {
        $image = imagecreatefromgif($source_url);
    } elseif ($info['mime'] == 'image/png') {
        $image = imagecreatefrompng($source_url);
    }
    imagejpeg($image, $destination_url, $quality);
    return $destination_url;
}

Problems: 问题:

  • If MIME is none of the above, the imagejpeg() will fail (but you didn't cater it; always check the return value of function) 如果MIME不满足上述条件,则imagejpeg()将会失败(但您并未满足要求;请始终检查函数的返回值)
  • you didn't check $destination_url is writable & exist or not 您没有检查$destination_url是否可写且存在或不存在

Next, assumes the compress_image() works well and returns $destination_url with a valid JPEG created at the path, the following codes cause further problems: 接下来,假设compress_image()运作良好,并返回$destination_url并在路径上创建了有效的JPEG,以下代码会引起进一步的问题:

$sql = "INSERT INTO fffdf (`user_id`,`imit`) VALUES ('9','$image')";
if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

Problems: 问题:

  • Why do you save the $image directly into DB? 为什么将$image直接保存到DB中? It's a very bad practice to store image data in DB. 将图像数据存储在DB中是非常糟糕的做法。 Save the path instead whenever possible 尽可能保存路径
  • $image is inaccessible here; $image在这里无法访问; the scope of the $image stays in the function compress_image() , thus $image still contains the value before compress_image() , unless you use global $image; $image的范围保留在compress_image()函数中,因此$image仍包含compress_image()之前的值,除非您使用global $image; in the compress function (which is not suggested). 在compress函数中(不建议使用)。 Pass the $image as function parameters by reference: 通过引用将$image作为函数参数传递:

    function compress_image(&$image, $destination_url, $quality) 函数compress_image(&$ image,$ destination_url,$ quality)

you don't really need the $source_url if you have the image data stored in $image . 如果您将图像数据存储在$image则实际上不需要$source_url

Hope the above helps. 希望以上内容对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM