-2

I got a problem with fileuploading images.

When I try to upload I get this error:

Warning: copy(/assets/img/products/): failed to open stream: No such file or directory in /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php on line 212

This is the code it's referring to:

if(isset($_POST['submitMoreImg'])){

    $name = $_POST['imgName'];
    $prod_id = $_POST['prod_id'];

    if(!empty($_FILES['image']))
    {
        $path = "/assets/img/products/";
        $path = $path.basename( $_FILES['image']['imgName']);
        if(copy($_FILES['image']['tmp_name'], $path)) {
            echo'<script type="text/javascript">
                    alert("uploaded");
                </script>';
              uploadExtraImg($name, $prod_id);
        } 
        else{
            echo "Error: ".$sql."<br>".$connection->error;
        }
      }
}

I can't seem to find the correct folder, I tried a lot of different folder path.

halfer
  • 19,824
  • 17
  • 99
  • 186
boyler211
  • 1
  • 3
  • The error message is pretty straight-forward. `/assets/img/products/ No such file or directory` The directory you are trying to move files to must first exist. – Matt Clark Dec 11 '17 at 22:10
  • Are you sure there's an `/assets` directory on your server? Note that `/` here means "root directory" not "webroot". – tadman Dec 11 '17 at 22:11
  • 1
    Possible duplicate of [PHP - Failed to open stream : No such file or directory](https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Harman Kamboj Dec 11 '17 at 22:12
  • Yea i know /assets dont exist on server, but i even tried copying the entire path with customer/d/7 and so on, and it says the same. How do i find the folders location on the server? – boyler211 Dec 11 '17 at 22:13
  • im not including anything, trying to upload a file to the folder on the server @HarmanKamboj – boyler211 Dec 11 '17 at 22:14
  • the path i know is: (website name)/newSite/assets/img/products/ But that wont work either – boyler211 Dec 11 '17 at 22:15

1 Answers1

0

The issue is apparent.

Warning: copy(/assets/img/products/): failed to open stream: No such file or directory in /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php on line 212

It's looking in directory /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php

But I'm hoping that you want to get /assets/img/products/customers/d/7/4 or as a complete path (website name)/httpd.www/newSite/assets/img/products/customers/d/7/4

In that case. Please make sure your path is correct.

try changing your $path value. Like so

<?php
if(isset($_POST['submitMoreImg'])){

    $name = $_POST['imgName'];
    $prod_id = $_POST['prod_id'];

    if(!empty($_FILES['image']))
    {
        $path = __DIR__."/../../../assets/img/products/";
        $path = $path.basename( $_FILES['image']['imgName']) + $fileNameWithExtension;
        if(copy($_FILES['image']['tmp_name'], $path)) {
            echo'<script type="text/javascript">
                    alert("uploaded");
                </script>';
              uploadExtraImg($name, $prod_id);
        } 
        else{
            echo "Error: ".$sql."<br>".$connection->error;
        }
      }
}
?>
Harman Kamboj
  • 429
  • 7
  • 20
  • Make sure your call is looking similar to this `copy('files/image.png', 'files/'."$path".'/img.png')` for it might actually be a syntax issue or you might be calling the wrong directory. As a piece of advice echo the $path value and try to access it directly from the browser. – Harman Kamboj Dec 12 '17 at 00:45
  • It works now. Had to change the $path to : $path = $path.basename( $_FILES['image']['name']); Thank u so much for the help! – boyler211 Dec 12 '17 at 00:52