1

I am trying to upload photos in to folder uploads and its path to be recorded under photo in DB. This is my code:

$folder ="uploads";
$destFile = $folder . basename($_FILES["photo"]["name"]);
$sourdeFile = $_FILES["photo"]["tmp_name"];


if(move_uploaded_file($sourdeFile,$destFile)){
    echo "File has been uploaded";
    $photo = $destFile;
}else{
    echo $_FILES['photo']['error'];
    $photo = "images/default.png";
}

When I upload photos they successfully uploaded into folder but the problem is its path recorded as follow :

uploads42141402_1866830986743601_8538143552767524864_n.jpg

But to view photos in a page there should be \ next to uploads. So I tried to change my code as follow.

$folder = "uploads\";

But it generates this error

Can anyone say how to fix this ?

octano
  • 851
  • 1
  • 10
  • 18

2 Answers2

0

How about

$folder ="uploads/";

I've tried and it works. Maybe $folder ="uploads" . DIRECTORY_SEPARATOR; will better than "/".

Van Tho
  • 618
  • 7
  • 20
  • Ahhh, I got why. How about [this link](https://stackoverflow.com/questions/10951334/php-move-uploaded-file-failed-to-open-stream-no-such-file-or-directory) – Van Tho Nov 15 '18 at 08:18
0

For the error its looks uploads folder is not there or it not have permission

create a upload folder or try with absolute path

you can make use of dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] for creating dynamic path

Mahesh Hegde
  • 1,131
  • 10
  • 12