Error inserting image into folder

Asked

Viewed 97 times

0

I am inserting an image into the Database. Only the image name is stored in the database, and the image is stored in a folder.

 $target_dir = "./uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    // if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
    //     echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    // } else {
    //     echo "Sorry, there was an error uploading your file.";
    // }
}


move_uploaded_file($_FILES["image"]["tmp_name"],"./uploads/" . $_FILES["image"]["name"]);            
$location=$_FILES["image"]["name"];

The image name is well inserted in the database, and only does not put the image in the folder.

Warning: move_uploaded_file(uploads/t-shirt.jpg): failed to open stream: No such file or directory in C: xampp htdocs Site user functions.php on line 262

Warning: move_uploaded_file(): Unable to move 'C: xampp tmp phpBB47.tmp' to 'uploads/t-shirt.jpg' in C: xampp htdocs Site user functions.php on line 262

Can you help me?

EDIT

Form Code :

<form action="registo_produtos.php" method="post" enctype="multipart/form-data">

<input type="hidden" name="size" value="1000000">

<label>Nome Produto : </label>
<input type=text size=70 name="NomeProduto" value="<?php echo $NomeProduto; ?>">
<text class="informacao">( NÃO INTRODUZIR ACENTOS NAS PALAVRAS )</text>
<br>
<label>Preço Produto : </label>
<br>
<input class="preco_produto" id="input" type="number" name="PrecoProduto" onkeypress="return allowOnlyNumbers(event)" value="<?php echo $PrecoProduto; ?>"/>
<text class="informacao">( NÃO INTRODUZIR O SÍMBOLO DO EURO € )</text>
<script>
function myFunction() {
    var str = "$PrecoProduto";
    var patt1 = /[^€]/gi; 
    var result = str.match(patt1);
}
</script>
<br>
<label>Imagem Produto : </label>
 
<input type="file" name="image">
<br>
<label>Descrição Produto : </label>
<textarea rows="4" cols="50" name="DescricaoProduto" value="<?php echo $DescricaoProduto; ?>"></textarea>
<br>
<label>Quantidade Produto : </label>
<select  name="QuantidadeProduto" value="<?php echo $QuantidadeProduto; ?>">
  <option selected data-default>- Indique -</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
  <option value="16">16</option>
  <option value="17">17</option>
  <option value="18">18</option>
  <option value="19">19</option>
  <option value="20">20</option>
  <option value="21">21</option>
  <option value="22">22</option>
  <option value="23">23</option>
  <option value="24">24</option>
  <option value="25">25</option>
  <option value="26">26</option>
  <option value="27">27</option>
  <option value="28">28</option>
  <option value="29">29</option>
  <option value="30">30</option>
  <option value="31">31</option>
  <option value="32">32</option>
  <option value="33">33</option>
  <option value="34">34</option>
  <option value="35">35</option>
  <option value="36">36</option>
  <option value="37">37</option>
  <option value="38">38</option>
  <option value="39">39</option>
  <option value="40">40</option>
  <option value="41">41</option>
  <option value="42">42</option>
  <option value="43">43</option>
  <option value="44">44</option>
  <option value="45">45</option>
  <option value="46">46</option>
  <option value="47">47</option>
  <option value="48">48</option>
  <option value="49">49</option>
  <option value="50">50</option>
  <option value="51">51</option>
  <option value="52">52</option>
  <option value="53">53</option>
  <option value="54">54</option>
  <option value="55">55</option>
  <option value="56">56</option>
  <option value="57">57</option>
  <option value="58">58</option>
  <option value="59">59</option>
  <option value="60">60</option>
  <option value="61">61</option>
  <option value="62">62</option>
  <option value="63">63</option>
  <option value="64">64</option>
  <option value="65">65</option>
  <option value="66">66</option>
  <option value="67">67</option>
  <option value="68">68</option>
  <option value="69">69</option>
  <option value="70">70</option>
  <option value="71">71</option>
  <option value="72">72</option>
  <option value="73">73</option>
  <option value="74">74</option>
  <option value="75">75</option>
  <option value="76">76</option>
  <option value="77">77</option>
  <option value="78">78</option>
  <option value="79">79</option>
  <option value="80">80</option>
  <option value="81">81</option>
  <option value="82">82</option>
  <option value="83">83</option>
  <option value="84">84</option>
  <option value="85">85</option>
  <option value="86">86</option>
  <option value="87">87</option>
  <option value="88">88</option>
  <option value="89">89</option>
  <option value="90">90</option>
  <option value="91">91</option>
  <option value="92">92</option>
  <option value="93">93</option>
  <option value="94">94</option>
  <option value="95">95</option>
  <option value="96">96</option>
  <option value="97">97</option>
  <option value="98">98</option>
  <option value="99">99</option>
  <option value="100">100</option>
</select>
<br>
<label>Categoria Produto : </label>
<select name="CategoriaProduto" value="<?php echo $CategoriaProduto; ?>">
  <option selected data-default>- Selecione -</option>
  <option value="Roupa Desportiva">Roupa Desportiva</option>
  <option value="Futebol">Futebol</option>
  <option value="Basquetebol">Basquetebol</option>
  <option value="Andebol">Andebol</option>
  <option value="Tenis">Tenis</option>
  <option value="Corfebol">Corfebol</option>
</select>
<br>
<button type="submit" class="btn" name="addproducts_btn">Adicionar Produtos</button>
</form>
  • On the line of move_uploaded_file(), "./uploads/" .was supposed to have a point before the /uploads?

  • is equal to have or not , is not hence the problem

  • The uploads folder is in the same directory as the uploading file?

  • yes it is in the same folder. I know that being on the same page is not necessary the '.' I just put it to see if it solved

  • try adding a basename() to the upload basename line($_FILES["image"]["name"]) Although I don’t think that’s it

  • I suspect it is permission... Permission to upload folder for writing and reading

  • and how do you give this permission? basename() is not

  • Before, try using the absolute path, move_uploaded_file($_FILES["image"]["tmp_name"],realpath(dirname(FILE))."/uploads/" . $_FILES["image"]["name"]); or if it doesn’t work try move_uploaded_file($_FILES["image"]["tmp_name"],"C:/xampp/htdocs/Site/user/uploads/". $FILES['image']['name']); as it is windows sometimes of these problems

Show 3 more comments

1 answer

-1

Dude, I got a code to insert images into the comic right now. If you want to take a look, I don’t have time right now so I can’t take a look at your code, but anyway, my purpose is to help.

if(isset($_FILES['imagem'])){
    $endereco = "img/".$id_usu."-foto".substr($_FILES['imagem']['name'], -4);
    $imagem = $id_usu."-foto".substr($_FILES['imagem']['name'], -4);
    $imageArr = array(
        'foto' => $imagem
    );

    if(move_uploaded_file($_FILES['imagem']['tmp_name'], $endereco)){
        DBUpdate('cadastros', $imageArr, " WHERE id = '$id_usu'");
    }
}
  • that just inserts the image? does not put in a folder?

  • With the code only to move to the folder I used this: if(isset($_FILES['image'])){ $address = "img/". $_FILES['image']['name']; move_uploaded_file($_FILES['image']['tmp_name'], $address); }

  • No, it works like your system, it just keeps track of the image in the BD. Only in mine I’m not doing as many checks as your code.

  • Just as a precaution, see if you put an enctype="Multipart/form-data" inside the form you send the image, without it I can’t save any image either

  • Dude, I took your code and put it on a test page and it worked normally

  • Take a look if the problem is not in the same form

  • I already put the form code on top

  • Did you test me code to just send the image to the directory? It worked?

  • its code did not solve, having even ceased to put the name of the image in my database. thing that was already made

  • I took your new HTML Edit that you’re using and it all worked out. It must be Windows permission error as Anderson Henrique said or it may be that you created the uploads folder before running the code with the wrong name, with me it only gives this error when or not create the folder or create with the wrong name, see if it is ok...

  • It may be Path problem as already said @Joségomes, make the change I asked putting the absolute path by being windows... we are here to help, but if not test what we say then it makes no sense

  • @Andersonhenrique I tested with absolute path and also gave error

  • @Higorcardoso your code gives error in entering name in database and does not put anything in folder

  • Dude, I think you better run your code on another PC to test it, because in my case, both yours and my code are working normally. But it only makes a deal before, comments here both the exact directory where the photos will be located and where is the PHP file that is uploading the photo.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.