-1

Having recently entered the world of dynamic website creation, I have been puzzled with trying to store the full path of an image into a MySQL table.

For me, creating a form with both "text" inputs and "file" inputs is proving to be problematic. For some god awful reason, I cannot retrieve the full path of the stored images, which I need in order to then display dynamically on my site.

So far, I have had to use W3 School's File Upload script http://www.w3schools.com/php/php_file_upload.asp for people to upload their images to a folder on my site's root which I called "uploads", but I have to reference the images manually one by one using a HTML table. Way too much work.

Here is the full code I am using (the HTML form, and PHP scripts needed to validate/sanitize user input and then connect to the database and insert the data into a table I called "images").

<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();

?>

This code allows me to save ONLY the folder in which the file was saved and not the full path. file_uploads is set to 1 in my php.ini file however display_errors is set to 0 (I'm using a free web host).

Logically, $target_file should contain the full path to the file (including the file name), but it only displays the folder in which the file was uploaded in the name column of the "images" table.

Thomas66
  • 1
  • 1

1 Answers1

0

$target_file is your variable

PHP

$curUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$fullPath = "$curUrl" . "$target_file";
divy3993
  • 5,732
  • 2
  • 28
  • 41
  • Hmmm, again the file name is empty, even though I see this path: http://rarq.byethost6.com/test (URL to my website, plus file name of the PHP file without extension). I'll keep looking for stuff but it seems everything I've tried has failed so far. – Thomas66 Jul 12 '15 at 20:38