简体   繁体   English

如果使用PHP已经存在文件,如何停止上传到Google Cloud Storage?

[英]How to stop upload to Google Cloud Storage if file already exists using PHP?

I'm having a problem with GAE and GCS. 我在GAE和GCS方面遇到问题。

I'm trying to upload a file, which works - but I can't seem to stop the upload if the file already exists. 我正在尝试上传一个可以正常工作的文件-但如果文件已经存在,我似乎无法停止上传。 It just gets a random temp name. 它只是获得一个随机的临时名称。

I have this code, but it doesn't do the job. 我有此代码,但无法完成工作。

<?php
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

SESSION_START();

$gs_new_name = $_FILES["uploaded_files"]['name'];
$image = CloudStorageTools::getPublicUrl("gs://bucket/".$gs_new_name, true);

if(getimagesize($image) !== FALSE){
    $gs_name = $_FILES["uploaded_files"]['tmp_name'];
    move_uploaded_file($gs_name, "gs://bucket/".$gs_new_name);
} else{
    die();
}

?>

It is not possible to stop an upload to GCS through PHP. 这是不可能通过PHP 停止上传到GCS。 When uploading directly to GCS via CloudStorageTools::createUploadUrl() the entire HTTP request is sent to the GCS proxy which handles the upload and then forwards the request back to your App Engine application. 通过CloudStorageTools::createUploadUrl()直接上传到GCS时,整个HTTP请求都会发送到处理上传的GCS代理,然后将请求转发回您的App Engine应用程序。 At that time PHP adds the location of the upload file on GCS to the $_FILES array. 那时,PHP将GCS上载文件的位置添加到$_FILES数组中。 As such the current approach of executing die() would not work since the entire upload has already been performed prior to the execution of your script. 因此,当前执行die()将不起作用,因为在执行脚本之前已执行了整个上载。

Short of checking client side (via JavaScript) for a particular filename/extension (plenty of topics on that) I am not sure how you would do something like this. 缺少检查客户端(通过JavaScript)的特定文件名/扩展名(有关大量主题)的方法,我不确定您将如何执行此操作。 I am also not entirely sure what you are trying to accomplish. 我也不完全确定您要完成什么。 Are you simply looking to restrict files to image types only? 您是否只是在将文件限制为仅图像类型? If so checking the extension on client side and then performing a check similar to the one you have should do the trick. 如果是这样,请在客户端检查扩展名,然后执行与您所执行的检查类似的检查即可。 Only instead of calling die() you could call unlink($gs_name) to remove the temporary file. 只有代替调用die()您才能调用unlink($gs_name)删除临时文件。

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

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