2010-05-04 232 views
1

我有這樣的代碼,我一直在努力,但我有一個很難爲它工作。我做了一個,但它只適用於PHP 5.3,我意識到我的主機只支持PHP 5.0!我是想看看我是否能得到它在我的服務器正常工作,我只是失去了,累了笑有人可以幫我修復我的代碼嗎?

01,對不起計算器對我來說是一個新事物。不知道該怎麼想。作爲一個論壇或發佈問題的地方......嗯,我很抱歉對我提問的方式粗魯。

我不知道我能不能給我如何正確地插入目錄結構與我如何寫這個代碼的一些指導。我不知道如何告訴PHP在哪裏上傳我的文件和諸如此類的東西,我從一個朋友,誰幫我整理一下我的一些錯誤的一些幫助,但我仍然有對付的mkdir和鏈路丟失,斷開鏈接功能。這就是我想提到我的直覺嗎?

我知道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(./上傳/ saiyanz2k /圖片/橫幅/阿滋-wall.jpg)function.move上傳文件]:失敗打開流:權限在/services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php否認線112 警告:move_uploaded_file()以[function.move上傳-file]:無法在/services7/webpages/util/s/a/saiya.site移動 '/ /的Webdata/phpupload/phpVoIEQj服務' 到 './uploads/saiyanz2k/images/banner/azumanga-wall.jpg' .aplus.net/helixagent.com/public/upload2.php 112行

+2

歡迎使用stackoverflow。您需要更新您的帖子,確認您收到的實際錯誤,或者解釋哪些功能無法正常工作。 您提供的代碼看起來應該可以在php 5.3和5.0中使用 – 2010-05-04 06:34:34

+3

首先,您可以告訴我們它是如何失敗的。特別是說出你期望發生的事情和實際發生的事情,包括任何錯誤消息。這是相當多的代碼來通過。你應該嘗試創建一個最小的測試用例併發布,而不是把所有東西都扔在你的問題中。 – outis 2010-05-04 06:34:35

+2

就我而言,我發現發佈300行代碼以及基本上說「它已損壞,請修復它」的單個段落有點不禮貌。 (比較:「不粗魯」將是12行代碼以及3段,解釋它做了什麼,它應該做什麼以及你準確收到什麼錯誤)。 – Tomalak 2010-05-04 06:55:27

回答

0

一種方法是從代碼中檢查某個命令/函數是否可用。您可以使用function_exists函數,例如:

if (function_exists('date_default_timezone_set')) 
{ 
    date_default_timezone_set("GMT"); 
} 
else 
{ 
    echo 'date_default_timezone_set is not supported....'; 
} 
+0

好吧,它工作完美,如果我使用5.3和__DIR__,但由於我在5.0我得到 警告:move_uploaded_file(./ uploads/saiyanz2k/images/banner/azumanga-wall.jpg)[function.move-uploaded-file] :未能打開流:權限被拒絕在/services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php上線112 警告:move_uploaded_file()[函數。 move-uploaded-file]:無法在/ services7/webpages/util/s/a /中移動'/ services/webdata/phpupload/phpVoIEQj'到'./uploads/saiyanz2k/images/banner/azumanga-wall.jpg' saiya.site.aplus.net/helixagent.com/public/upload2.php 112行 – Eli 2010-05-04 07:13:22

+1

@ user267490:如果這是錯誤,它並不意味着功能不起作用,這意味着最有可能的是上傳的路徑文件錯誤或者php.ini文件中的上傳限制設置得非常低。 – Sarfraz 2010-05-04 07:14:55

+0

嗯,亞我認爲我的目錄結構沒有被正確完成。也許這是我真的很難與代碼。嗯,我的服務器上的目錄是/ uploads/$ username/images/banner /,我正在嘗試使用。 – Eli 2010-05-04 07:19:54

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

PHP <5.3中'__DIR__'等價於'dirname(__ FILE __);' - 錯誤提示您的目標目錄不可寫,這不是PHP的問題。此外,請編輯您的問題以提供更多信息。 SO不會像論壇一樣使用。 – Gordon 2010-05-04 07:17:50

+0

繼續戈登的評論,SO使用Q&A格式。聲稱是答案的帖子應該回答這個問題。關於這個問題的信息出現在問題中。其他帖子應該是評論。你可以隨時對自己的問題發表評論,並在其他問題中評論50次。閱讀常見問題(點擊頁面頂部的鏈接)。 – outis 2010-05-04 21:03:43

0

它看起來像你沒有訪問該文件夾(或文件)中的錯誤

/上傳/ $的用戶名/圖片/橫幅/ $文件名

這可能是因爲主機basedir限制(例如,你可能不會父級目錄/服務/ webdata /)或只是在操作系統的權限丟失。

嘗試(臨時)將/ uploads /的權限設置爲777或從控制檯執行腳本以查看您是否擁有basedir限制。

0

採取在錯誤信息的路徑仔細一看:

/uploads/saiyanz2k/images/banner/azumanga-wall.jpg
/services7/webpages/util/s/a/saiya.site.aplus.net/helixagent.com/public/upload2.php

的destination是一個相對路徑,很可能與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平臺上受支持。

相關問題