2015-07-12 29 views
-1

最近進入了動態網站創建的世界,我一直試圖將圖像的完整路徑存儲到MySQL表中。如何從窗體插入正確的圖像路徑到MySQL表中?

對我來說,創建一個既有「文本」輸入又有「文件」輸入的表單被證明是有問題的。對於某些神可怕的原因,我無法檢索存儲圖像的完整路徑,我需要這些路徑才能在我的網站上動態顯示。

到目前爲止,我不得不使用W3學校的文件上傳腳本http://www.w3schools.com/php/php_file_upload.asp讓人們將他們的圖像上傳到我的網站根目錄上的一個文件夾,我稱之爲「上傳」,但我必須逐個手動引用圖像一個HTML表格。工作太多了。

下面是我使用的完整代碼(HTML表單和PHP腳本,用於驗證/清理用戶輸入,然後連接到數據庫並將數據插入到名爲「圖像」的表格中)。

<form method="post" enctype="multipart/formdata" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
 
          <?php echo $nomimgerr;?> <input type="text" name="nomimg"> 
 
         <fieldset> <legend> Image &agrave; soumettre </legend> <input type="file" name="notimg"> 
 
         <input type="submit" value="Soumettre" name="submit"> </fieldset> </form>

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["notimg"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "D&eacute;sol&eacute;, le fichier existe d&eacute;j&agrave;."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["notimg"]["size"] > 1000000) { 
    echo "D&eacute;sol&eacute;, votre image est trop grosse."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) { 
    echo "D&eacute;sol&eacute;, seuls les formats JPG, JPEG et PNG sont permis."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "L'image n'as pas &eacute;t&eacute; upload&eacute;e."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["notimg"]["tmp_name"], $target_file)) { 
    echo "Le fichier ". basename($_FILES["notimg"]["name"]). " a &eacute;t&eacute; upload&eacute;."; 
} else { 
    echo "D&eacute;sol&eacute;, il y a eu une erreur en uploadant votre image."; 
} 
} 
?> 

       <?php 

       $nomimg = $nomimgerr = ""; 


       if ($_SERVER["REQUEST_METHOD"] == "POST") { 
         if (empty($_POST["nomimg"])) { 
         $nomimgerr = "Votre nom est requis"; 
         } else { 
         $nomimg = nettoyeur($_POST["nom"]); 
         // check if name only contains letters and whitespace 
         if (!preg_match("/^[a-zA-Z ]*$/",$nomimg)) { 
          $nomimgerr = "Seuls les lettres et les espaces sont permis"; 
         } 
         } 
       }  

        function nettoyeur($data) { 
          $data = trim($data); 
          $data = stripslashes($data); 
          $data = htmlspecialchars($data); 
          return $data; 
         } 

       ?> 

<?php 
// MySQL connect 
include 'mysql_connect.php'; 

$nom = mysqli_real_escape_string($conn, $_POST['nom']);        

$sql = "INSERT INTO images (nom, name) VALUES ('$nomimg', '$target_file')"; 

$stmt = $conn->prepare("INSERT INTO images (nom, name) VALUES (?, ?)"); 
$stmt->bind_param("ss", $nomimg, $target_file); 


$stmt->execute(); 

$stmt->close(); 

if ($conn->query($sql) === TRUE) { 
    echo ""; 

} else { 
echo "Error: " . $sql . "<br>" . $conn->error; 
            } 



$conn->close(); 

?> 

此代碼可以讓我只保存在該文件中保存的文件夾,而不是完整路徑。 file_uploads在我的php.ini文件中設置爲1,但是display_errors設置爲0(我使用的是免費的Web主機)。

邏輯上,$ target_file應該包含文件的完整路徑(包括文件名),但它只顯示文件上載到「images」表的名稱列中的文件夾。

+0

你的意思是你的,全URL /文件夾/子文件夾/圖像? – divy3993

+0

請參閱此答案:http://stackoverflow.com/questions/6768793/get-the-full-url-in-php/8891890#8891890 – divy3993

+0

謝謝我會給它一看! – Thomas66

回答

0

$ TARGET_FILE是你的變量

PHP

$curUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$fullPath = "$curUrl" . "$target_file"; 
+0

嗯,再次文件名是空的,即使我看到這個路徑:http://rarq.byethost6.com/test(URL到我的網站,加上沒有擴展名的PHP文件的文件名)。我會繼續尋找東西,但似乎我所嘗試過的一切都失敗了。 – Thomas66