gpt4 book ai didi

php - 上传文件过程中不会导致服务器端错误

转载 作者:可可西里 更新时间:2023-11-01 13:21:33 26 4
gpt4 key购买 nike

我不断地对我的软件进行调整并将其上传到服务器。由于我的客户一直在使用我的在线软件,如果 PHP 文件部分上传(假设上传需要 3 秒),当客户单击链接(比如在 1 秒标记处)时,他们会收到错误消息,因为文件还在上传中...

Parse error: syntax error, unexpected $end in /inc/functions.php on line 475

因为我在澳大利亚,我们的互联网......嗯......“不是很快”是一个很好的表达方式。

上传文件时是否使用了任何技术,以免对使用该软件的客户造成错误?

我唯一能做的就是上传文件到另一个目录,然后运行一个 PHP 脚本,以超快的速度复制文件...但是有更好的解决方案吗?

最终代码

多亏了下面的 Greg,我才能够找出最好的做事方式。以为我会分享我的最终代码。这有点粗糙,但可以解决问题......希望它能帮助别人

<?php

// root path
define('ABSPATH', dirname(__FILE__) . '/');

// messages
$GLOBALS['copied'] = array();
$GLOBALS['failed'] = array();
$GLOBALS['folders'] = array();

// you have to submit the form (added security)
if (isset($_POST['copy'])) {

$GLOBALS['devuploads_folder'] = '_devuploads';

function find_files($dir) {
if ($dh = opendir(ABSPATH . $dir)) {
while (($file = readdir($dh)) !== false) {

// ignore files
if ($file === '.' || $file === '..')
continue;

// delete temporary files (optional)
if ($file == '.DS_Store') {
unlink(ABSPATH . $dir . $file);
continue;
}

// determine paths
$live_path = str_replace($GLOBALS['devuploads_folder'] . '/', '', $dir . $file);
$dev_file = $dir . $file;
$live_file = $live_path;
$dev_file_path = ABSPATH . $dir . $file;
$live_file_path = ABSPATH . $live_path;

// it's a file
if (is_file(ABSPATH . $dir . $file)) {

// check if the file has been updated or it's a brand newy
$updated_file = $new_file = false;
if (file_exists($live_file_path)) {
$dev_file_modified = filemtime($dev_file_path);
$live_file_modified = filemtime($live_file_path);
if ($dev_file_modified > $live_file_modified)
$updated_file = true;
} else {
$new_file = true;
}

// move the file
if ($updated_file || $new_file) {
if (copy($dev_file_path, $dev_file_path . '.bak')) {
if (rename($dev_file_path . '.bak', $live_file_path))
if ($new_file)
$GLOBALS['copied'][] = '<strong>New File:</strong> ' . $dev_file . ' moved to ' . $live_file;
else
$GLOBALS['copied'][] = $dev_file . ' moved to ' . $live_file;
else
$GLOBALS['failed'][] = '<strong>Rename failed:</strong> ' . $dev_file . ' to ' . $live_file;
} else {
$GLOBALS['failed'][] = '<strong>Copy failed:</strong> ' . $dev_file . ' to ' . $dev_file . '.bak';
}
}

// it's a folder
} else if (is_dir(ABSPATH . $dir . $file)) {

// create new folder if it doesn't exist
if (!is_dir($live_file_path)) {
$GLOBALS['folders'][] = '<strong>Created:</strong> ' . $live_file;
mkdir($live_file_path, 0755);
}

// keep digging
find_files($dir . $file . '/');

}

}
closedir($dh);
}
}

find_files($GLOBALS['devuploads_folder'] . '/');

}

?>
<!DOCTYPE HTML>
<html>
<head>
<title>Copy Changes</title>
<style type="text/css">
h1 {
font: normal 20px Arial, Helvetica, sans-serif;
line-height: 24px;
}
p, li {
font: normal 14px Arial, Helvetica, sans-serif;
line-height: 20px;
}
</style>
</head>
<body>

<?php
if (!empty($GLOBALS['failed'])) {
echo '<h1>Errors</h1>';
echo '<ul>';
foreach($GLOBALS['failed'] AS $message) {
echo '<li>' . $message . '</li>';
}
echo '</ul>';
}

if (!empty($GLOBALS['folders'])) {
echo '<h1>New Folders</h1>';
echo '<ul>';
foreach($GLOBALS['folders'] AS $message) {
echo '<li>' . $message . '</li>';
}
echo '</ul>';
}

if (!empty($GLOBALS['copied'])) {
echo '<h1>Copied</h1>';
echo '<ul>';
foreach($GLOBALS['copied'] AS $message) {
echo '<li>' . $message . '</li>';
}
echo '</ul>';
}

if (empty($GLOBALS['failed']) && empty($GLOBALS['folders']) && empty($GLOBALS['copied']))
echo '<p>No updates made.</p>';
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="copy" value="1" />
<p><input type="submit" value="Copy Files" /></p>
</form>

</body>
</html>

最佳答案

如果您的服务器是 Linux(或其他 Unix 变体),则 mv 命令是 atomic 并且可以进行这种即时更新。首先将文件复制到一个临时名称(如file.php.new),然后登录服务器并

mv file.php.new file.php

(即使 file.php 存在,它也能工作,它将被新的替换)。

关于php - 上传文件过程中不会导致服务器端错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com