如果使用PHP已经存在文件,如何停止上传到Google Cloud Storage? [英] How to stop upload to Google Cloud Storage if file already exists using PHP?

查看:41
本文介绍了如果使用PHP已经存在文件,如何停止上传到Google Cloud Storage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GAE和GCS方面遇到问题.

I'm having a problem with GAE and 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();
}

?>

推荐答案

通过PHP 停止上传到GCS是不可能的.通过 CloudStorageTools :: createUploadUrl()直接上传到GCS时,整个HTTP请求将发送到处理上传的GCS代理,然后将请求转发回您的App Engine应用程序.那时,PHP将上载文件在GCS上的位置添加到 $ _ FILES 数组中.因此,执行 die()的当前方法将不起作用,因为在执行脚本之前已经执行了整个上传.

It is not possible to stop an upload to GCS through PHP. 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. At that time PHP adds the location of the upload file on GCS to the $_FILES array. 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.

(通过JavaScript)检查客户端是否有特定的文件名/扩展名(大量有关此主题的内容)的简短内容我不确定您将如何做这样的事情.我也不完全确定您要完成什么.您是否只是在将文件限制为仅图像类型?如果是这样,请在客户端检查扩展名,然后执行与您所执行的检查类似的检查即可.只能调用 unlink($ gs_name)来删除临时文件,而不是调用 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. 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.

这篇关于如果使用PHP已经存在文件,如何停止上传到Google Cloud Storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆