5

I try to insert images in my database with PHP and MySQL with a temporary folder.

I use laravel and this is my controller:

if(isset($_FILES['img_masc']))
{
    $img=$_FILES['img_masc']['name'];
    $ruta= $_FILES['img_masc']['tmp_name'];
}

$destino='../../../Perf_Masc/'.$img;
$masc->img=$destino;
//copy($ruta, $destino);
move_uploaded_file($ruta, $destino); //line 49

This is my view:

<form method="POST" action="/RegMasc" enctype= "multipart/form-data" >
    <div>
        <input required name="img_masc" type="file"/>
    </div>

This is my error:

ErrorException in line 49: move_uploaded_file(../../../Perf_Masc/AF5.jpg): failed to open stream: No such file or directory

I try with so much things and also with the copy function and is not working anyway

Anto S
  • 2,448
  • 6
  • 32
  • 50
Valeria
  • 160
  • 1
  • 3
  • 12

2 Answers2

8

In your Config file or some common file define your path as below

define('DOCROOT', $_SERVER['DOCUMENT_ROOT'].'<YOUR PROJECT DIRECTORY>/');

Include this common php in all your class file.

Then

 $destino= DOCROOT.'Perf_Masc/'.$img; // HERE DOCROOT is defined in config.
Anto S
  • 2,448
  • 6
  • 32
  • 50
  • I put the define('DOCROOT', 'D:/wamp/www/total/'); in the top of the function where i am using the image stuff is that correct? Because that way doesn't work D: throw me this error now: move_uploaded_file(C:/wamp/www/total/Perf_Masc/AF5.jpg): failed to open stream: No such file or directory – Valeria Dec 31 '15 at 05:37
  • @Valeria `total` is my project name. You replace your project directory name – Anto S Dec 31 '15 at 05:43
  • Ohhh, i understand but if all my project is in 'C:/wamp/www/' how i write that? S: i am sorry but i am learning yet. D: – Valeria Dec 31 '15 at 05:52
  • C:/wamp/www// – Anto S Dec 31 '15 at 05:54
  • @Valeria refer my updated answer. In this way you need not to modify your codings when migrate server – Anto S Dec 31 '15 at 05:58
  • All the folders of my project are in C:/wamp/www/ because i use laravel and laravel pull apart the project in controllers, views and models, i dont have clear how i supposed to do that, so please be patience with me i'm learning – Valeria Dec 31 '15 at 06:04
  • @Valeria you mean to say you dont have any separate project folders? all your laravel folders inside `wamp/www`? Directory `Perf_Masc/` is inside `wamp/www/` – Anto S Dec 31 '15 at 06:07
  • Yep that's right D: because i start quicly the project and i am new in laravel. – Valeria Dec 31 '15 at 06:14
  • @Valeria then define as `define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);` and then `$destino=DOCROOT.'Perf_Masc/'.$img;` – Anto S Dec 31 '15 at 06:25
  • OMG thank you very much longer functions properly. Can you explain how? Only a little?. thanks anyway. :DD – Valeria Dec 31 '15 at 06:48
  • @Valeria before `move_upload_file()` doesn't find the destination directory. I mean `../../../Perf_Masc` doesn't meant the right path. – Anto S Dec 31 '15 at 06:54
  • Hmmm... but i try with more paths and nothing works. And now if you can help me with something else, i can't show the image in my page u_u echo(' '); $img_masc it's the address where the image is. – Valeria Dec 31 '15 at 08:08
  • @Valeria what is not working? Upload is not working or image uploaded but not displating properly? – Anto S Dec 31 '15 at 08:10
  • The path of the image is correctly uploaded to the database and the images also are copied correctly in the folder. But i can't see the image in the page. – Valeria Dec 31 '15 at 08:16
  • echo(' '); //$img_masc it's the address where the image is. – Valeria Dec 31 '15 at 08:19
  • @Valeria try echo ''; – Anto S Dec 31 '15 at 08:53
  • It didn't work, but appear a broken little image, but shouldn't show that. – Valeria Dec 31 '15 at 08:58
  • @Valeria using console just check whther you are getting the image name. Print `$img_masc` and see whats printing on this variable. Debug step by step – Anto S Dec 31 '15 at 09:02
  • I already did that and prints: "c:/wamp/www/Perf_Masc/image.jpg" but i dont know why dont show the imagen if looks good. I think dont find the route por i dont know. – Valeria Dec 31 '15 at 09:13
  • @Valeria inside image src if you pass like c:/wamp/www/Perf_Masc/image.jpg it wont work. Inside image src it should be like – Anto S Dec 31 '15 at 10:26
  • I try with and doesnt work. D: but with does work. – Valeria Dec 31 '15 at 18:49
  • There is now a troubleshooting checklist for this frequent error here : https://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:35
1

Change your forward slash to a back slash.

First define these

//Define back slash so that you can use it anywhere later
defined("DS") ? null : define("DS", DIRECTORY_SEPARATOR);
// Define your website siteroot
defined("SITE_ROOT") ? null : define("SITE_ROOT", "C:".DS."wamp".DS."www".DS."your_website");

Now move your files

$file_name=$_FILES['file']['name'];
$file_tmp=$_FILES['file']['tmp_name'];
$file_upload_to=SITE_ROOT . DS . "Perf_Masc";

move_uploaded_files($file_tmp, $file_upload_to . DS . $file_name);

If you have a hard time defining the root of your website, you can create a php file in your root directory and then echo __DIR__ for PHP 5.3 or later, for earlier versions use echo dirname(__FILE__).

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77