使用 PHP 创建的 Zip 文件显示在本地主机上,但不显示在服务器上

发布于 2024-10-25 06:19:07 字数 1755 浏览 3 评论 0原文

我是 PHP 新手,这是我的困境。我正在尝试向我的网站添加一项功能,允许用户在 Flash (AS3) 中选择一组图像,单击按钮,然后让我的服务器创建所有图像的 zip 文件供他们下载。

我已经在 flash 中编写了我的代码,并打包了我的 php 脚本,并在我的本地主机上对其进行了测试,一切都运行良好。但是当我将相同的文件上传到我的服务器时,不会创建 zip 文件。下面是我的 flash 和 php 的代码。任何帮助或提示将不胜感激,我正在为她而苦苦挣扎。

本地主机服务器使用的是 5.2.17,因此如果我的服务器托管在 1&1

Flash 代码将信息发送到 PHP

function MakeZip():void
{
var variables:URLVariables = new URLVariables();
//variables.Image1=img1;
variables.ImageList=OrderItems;
variables.Name= zipName_txt.text + ".zip";
variables.FileComplete="File Created";

var request:URLRequest = new URLRequest();

request.url="ArrayTest.php";

request.data=variables;
var loader:URLLoader = new URLLoader();

loader.load(request);//sends the request 
//when the request is done loading, it goes to the completeWriting function
loader.addEventListener(Event.COMPLETE, completeWriting);
loader.addEventListener(IOErrorEvent.IO_ERROR, error);

function completeWriting(event:Event):void
{
    info_txt.text = "File Created";
}

function error(e:IOErrorEvent):void
{
    info_txt.text = "There was an error. Please try again later.";
}

}

PHP 创建文件

<?PHP

$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];


// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f"); 
print false;  
}

// close and save archive
$zip->close();
//writeVariable( "ZipComplete", $ZipComplete);
echo "FileComple=" . $ZipComplete; 

?> 

I am new to PHP and here is my dilemma. I am trying to add to my site a feature that allows users to select a set of images in Flash (AS3) click a button and have my server create a zip file of all the images for them to download.

I have already wrote my code in flash and crated my php script and tested it on my localhost and everything works great. But when I upload the same files to my sever no zip files are created. Below is the code for both my flash and for my php. Any help or tips would be appreciated I am struggling over her.

Localhost server is using 5.2.17 and so if my Server hosted on 1&1

Flash Code To Send Info to PHP

function MakeZip():void
{
var variables:URLVariables = new URLVariables();
//variables.Image1=img1;
variables.ImageList=OrderItems;
variables.Name= zipName_txt.text + ".zip";
variables.FileComplete="File Created";

var request:URLRequest = new URLRequest();

request.url="ArrayTest.php";

request.data=variables;
var loader:URLLoader = new URLLoader();

loader.load(request);//sends the request 
//when the request is done loading, it goes to the completeWriting function
loader.addEventListener(Event.COMPLETE, completeWriting);
loader.addEventListener(IOErrorEvent.IO_ERROR, error);

function completeWriting(event:Event):void
{
    info_txt.text = "File Created";
}

function error(e:IOErrorEvent):void
{
    info_txt.text = "There was an error. Please try again later.";
}

}

PHP to create file

<?PHP

$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];


// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f"); 
print false;  
}

// close and save archive
$zip->close();
//writeVariable( "ZipComplete", $ZipComplete);
echo "FileComple=" . $ZipComplete; 

?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-11-01 06:19:07

感谢大家的提示和提示。经过几个小时的尝试不同的事情后,我发现错误出在我的 Flash 代码中。在我的本地主机上,它忽略文件名的大写。但在我的服务器上它不会忽略它。我的 Flash 文件中有 image1.jpg,但文件名实际上是 Image1.jpg。很高兴我发现它有点令人沮丧,这样的小事让它不起作用。

Thank you all for your tips and hints. After hours of trying different things I found the mistake was in my flash code. On my localhost it ignores capitalization for file names. On my server however it does not ignore it. I had in my flash file image1.jpg but the file name is actually Image1.jpg. Glad I found it bit it is so frustrating how a little thing like that made it not work.

抚你发端 2024-11-01 06:19:07

你的php脚本有返回错误响应吗?
请确保服务器上的zip文件保存路径正确并且具有写入权限

is there any error response returned from you php script?
please make sure the zip file save path on server is correct and have write access

蒗幽 2024-11-01 06:19:07

这是因为代码在服务器上以“无人”身份运行。该用户对除 /tmp 之外的任何地方都没有写权限。

添加以下代码,这应该可以工作:

<?PHP

$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];


// create object
$zip = new ZipArchive();

$zipName = "/tmp/" + $zipName;

// open archive 
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

$zipName = "/tmp/" + $zipName; 是关键部分。

This is because code is running as "nobody" on server. This user would not have write access to anywhere except /tmp .

Add following code, this should work:

<?PHP

$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];


// create object
$zip = new ZipArchive();

$zipName = "/tmp/" + $zipName;

// open archive 
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

$zipName = "/tmp/" + $zipName; is the key part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文