我有这个代码我一直在努力,但我很难工作。我做了一个,但它只适用于PHP 5.3,我意识到我的主机只支持PHP 5.0!我是否正试图看看我是否可以正确使用它在我的服务器上工作,我只是迷路了,累了哈哈
对不起,抱歉stackoverflow对我来说是个新事物。不知道怎么想。作为一个论坛或发布问题的地方...嗯,我很抱歉用我的方法粗鲁。我想知道你可以给我一些关于如何正确插入目录结构的指导,以及我如何编写这段代码。我不知道如何告诉PHP在哪里上传我的文件和诸如此类的东西,我得到了一位朋友的帮助,他帮助我理清了我的一些错误,但我仍然迷失在处理mkdir和链接,unlink功能。这是我怎么想提到我的指示?
我知道php 5.3使用_ DIR _和php 5.0使用dirname(_ _ FILE_ _),我已经尝试了两种,我得到了同样的错误。我的文件设置为0777用于测试目的。现在想要编写和移动我上传的文件可能会出现什么问题?
} elseif ( (file_exists("\\uploads\\{$username}\\images\\banner\\{$filename}")) || (file_exists("\\uploads\\{$username}\\images\\banner\\thumbs\\{$filename}")) ) {
$errors['img_fileexists'] = true;
}
if (! empty($errors)) {
unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file
}
// Create thumbnail
if (empty($errors)) {
// Make directory if it doesn't exist
if (!is_dir("\\uploads\\{$username}\\images\\banner\\thumbs\\")) {
// Take directory and break it down into folders
$dir = "uploads\\{$username}\\images\\banner\\thumbs";
$folders = explode("\\", $dir);
// Create directory, adding folders as necessary as we go (ignore mkdir() errors, we'll check existance of full dir in a sec)
$dirTmp = '';
foreach ($folders as $fldr) {
if ($dirTmp != '') { $dirTmp .= "\\"; }
$dirTmp .= $fldr;
mkdir("\\".$dirTmp); //ignoring errors deliberately!
}
// Check again whether it exists
if (!is_dir("\\uploads\\$username\\images\\banner\\thumbs\\")) {
$errors['move_source'] = true;
unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file
}
}
if (empty($errors)) {
// Move uploaded file to final destination
if (! move_uploaded_file($_FILES[IMG_FIELD_NAME]['tmp_name'], "/uploads/$username/images/banner/$filename")) {
$errors['move_source'] = true;
unlink($_FILES[IMG_FIELD_NAME]['tmp_name']); //cleanup: delete temp file
} else {
// Create thumbnail in new dir
if (! make_thumb("/uploads/$username/images/banner/$filename", "/uploads/$username/images/banner/thumbs/$filename")) {
$errors['thumb'] = true;
unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
}
}
}
}
// Record in database
if (empty($errors)) {
// Find existing record and delete existing images
$sql = "SELECT `bannerORIGINAL`, `bannerTHUMB` FROM `agent_settings` WHERE (`agent_id`={$user_id}) LIMIT 1";
$result = mysql_query($sql);
if (!$result) {
unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
unlink("/uploads/$username/images/banner/thumbs/$filename"); //cleanup: delete thumbnail file
die("<div><b>Error: Problem occurred with Database Query!</b><br /><br /><b>File:</b> " . __FILE__ . "<br /><b>Line:</b> " . __LINE__ . "<br /><b>MySQL Error Num:</b> " . mysql_errno() . "<br /><b>MySQL Error:</b> " . mysql_error() . "</div>");
}
$numResults = mysql_num_rows($result);
if ($numResults == 1) {
$row = mysql_fetch_assoc($result);
// Delete old files
unlink("/uploads/$username/images/banner/" . $row['bannerORIGINAL']); //delete OLD source file
unlink("/uploads/$username/images/banner/thumbs/" . $row['bannerTHUMB']); //delete OLD thumbnail file
}
// Update/create record with new images
if ($numResults == 1) {
$sql = "INSERT INTO `agent_settings` (`agent_id`, `bannerORIGINAL`, `bannerTHUMB`) VALUES ({$user_id}, '/uploads/$username/images/banner/$filename', '/uploads/$username/images/banner/thumbs/$filename')";
} else {
$sql = "UPDATE `agent_settings` SET `bannerORIGINAL`='/uploads/$username/images/banner/$filename', `bannerTHUMB`='/uploads/$username/images/banner/thumbs/$filename' WHERE (`agent_id`={$user_id})";
}
$result = mysql_query($sql);
if (!$result) {
unlink("/uploads/$username/images/banner/$filename"); //cleanup: delete source file
unlink("/uploads/$username/images/banner/thumbs/$filename"); //cleanup: delete thumbnail file
die("<div><b>Error: Problem occurred with Database Query!</b><br /><br /><b>File:</b> " . __FILE__ . "<br /><b>Line:</b> " . __LINE__ . "<br /><b>MySQL Error Num:</b> " . mysql_errno() . "<br /><b>MySQL Error:</b> " . mysql_error() . "</div>");
}
}
// Print success message and how the thumbnail image created
if (empty($errors)) {
echo "<p>Thumbnail created Successfully!</p>\n";
echo "<img src=\"/uploads/$username/images/banner/thumbs/$filename\" alt=\"New image thumbnail\" />\n";
echo "<br />\n";
}
}
我收到以下错误:
警告:move_uploaded_file(./ uploads / saiyanz2k / images / banner / azumanga-wall.jpg)[function.move-uploaded-file]:无法打开流:/ services7 / webpages / util / s /中的权限被拒绝第112行的a / saiya.site.aplus.net / helixagent.com / public / upload2.php 警告:move_uploaded_file()[function.move-uploaded-file]:无法将'/ services / webdata / phpupload / phpVoIEQj'移动到/ services7 /中的'./uploads/saiyanz2k/images/banner/azumanga-wall.jpg'第112行的webpages / util / s / a / saiya.site.aplus.net / helixagent.com / public / upload2.php
答案 0 :(得分:0)
一种方法是从代码中检查某个命令/功能是否可用。您可以使用function_exists
函数,例如:
if (function_exists('date_default_timezone_set'))
{
date_default_timezone_set("GMT");
}
else
{
echo 'date_default_timezone_set is not supported....';
}
答案 1 :(得分:0)
啊!对不起,我的意思并不是要向你们发泄我的挫败感。但是我现在已经看了好几个小时了。
就像我提到的这段代码有效,但由于我的服务器很挑剔,我无法使用我编码的5.3语法。这是我试图让它在我的服务器的5.0 php上工作。
特别是我认为mkdir()和unlink()函数有问题。
如果你去www.helixagent.com登录测试/测试,然后在网址中找到/upload2.php然后你会看到它向我投掷的错误。
嗯,如果我使用5.3和 DIR 它会很完美但是因为我在5.0上我尝试了不同的方法
我得到的错误是
Warning: move_uploaded_file(./uploads/saiyanz2k/images/banner/azumanga-wall.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/services/webdata/phpupload/phpVoIEQj' to './uploads/saiyanz2k/images/banner/azumanga-wall.jpg' in /services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php on line 112
答案 2 :(得分:0)
您似乎无权访问文件夹(或文件)
/上传/ $用户名/图像/横幅/ $文件名
这可能是因为对主机的基础限制(例如,您可能不会将父目录/ services / webdata /留下来)或只是缺少权限。
尝试(临时)将/ uploads /的权限设置为777或从控制台执行脚本以查看是否有基础限制。
答案 3 :(得分:0)
仔细查看错误消息中的路径:
即可。强> /uploads/saiyanz2k/images/banner/azumanga-wall.jpg
/services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php
目的地是相对路径,很可能是相对于upload2.php的目录。我看到的一条相对路径就是这条线:
// Take directory and break it down into folders
$dir = "uploads\\{$username}\\images\\banner\\thumbs";
应该是:
// Take directory and break it down into folders
$dir = "\\uploads\\{$username}\\images\\banner\\thumbs";
实际上,它应该是
$dir = "/uploads/{$username}/images/banner/thumbs";
因为PHP支持在所有平台上使用正斜杠作为目录分隔符,而反斜杠仅在MS平台上支持。