Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move_uploaded_file(...): failed to open stream: No such file or directory

Tags:

php

mysql

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

like image 317
Valeria Avatar asked Dec 31 '15 05:12

Valeria


2 Answers

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.
like image 73
Butterfly Avatar answered Nov 03 '22 13:11

Butterfly


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__).

like image 33
Rusty Avatar answered Nov 03 '22 14:11

Rusty