如何从窗体插入正确的图像路径到MySQL表中?

问题描述:

最近进入了动态网站创建的世界,我一直试图将图像的完整路径存储到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

$ TARGET_FILE是你的变量

PHP

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

嗯,再次文件名是空的,即使我看到这个路径:http://rarq.byethost6.com/test(URL到我的网站,加上没有扩展名的PHP文件的文件名)。我会继续寻找东西,但似乎我所尝试过的一切都失败了。 – Thomas66