harrywonder12's avatar

Laravel File Error

I'm using the latest version of laravel and when I try to upload an image, the image gets moved to the specified directory but the function returns a error saying temp/some-code does not exist or might have been moved.

0 likes
3 replies
wilburpowery's avatar

If you just pasted the entire error it might be easier to help you :)

siangboon's avatar

Questioning 101, show the error if you encountered, show the code if it help...

Guessing works are tired and time consuming.... the worst part is it may guessed wrong also....

harrywonder12's avatar

Here is the code that throws the error

try { foreach ($request->allFiles('product_image') as $productImage) {

        foreach ($productImage as $key) {
          //parse the file and upload it to a new directory while saving it...
          //The images are stored as a json string to the database...
          $productImageName = $key->getClientOriginalName();
          $productImageExtension = $key->getClientOriginalExtension();
          //make an attempt to rename the resource...
          $productImageNameArr = explode('.', $productImageName);
          array_pop($productImageNameArr);
          $newProductImageName = implode('_', $productImageNameArr) . time() . '.' . $productImageExtension;
          //move the resource...
          $key->move("images/products", $newProductImageName);
          array_push($images_images, "images/products/" . $newProductImageName);
        }

      }
    } catch (Exception $e) {
      dd($e, 'Fail one Default');
      //Bail in case of an error.... and clear the images created before the erro...
      foreach ($images_images as $image) {
        if (File::exists($image)) {
          File::delete($image);
        }
      } //all images created during the operation has been deleted...
      return redirect()->back()->with('error', 'Sorry... An unexpected error occurred and the operation could not be completed. Please, try again later...');
    }

Please or to participate in this conversation.