mozew's avatar
Level 6

The file "C:\xampp\tmp\php8911.tmp" does not exist

I after install

composer require intervention/image

I want to upload image and after submit a form, I get this error.

The file "C:\xampp\tmp\php1D5F.tmp" does not exist

public function store(ArticleRequest $request)
{
    auth()->loginUsingId(1);
    $imagesUrl = $this->uploadImages($request->file('images'));

    $article = auth()->user()->article()->create(array_merge($request->all(), ['images' => $imagesUrl]));
    $article->categories()->attach(request('category'));


    return redirect(route('articles.index'));
}

protected function uploadImages($file)
{
    $year = Carbon::now()->year;
    $imagePath = "/upload/images/{$year}/";
    $filename = $file->getClientOriginalName();

    $file = $file->move(public_path($imagePath) , $filename);

    $sizes = ["300" , "600" , "900"];
    $url['images'] = $this->resize($file->getRealPath() , $sizes , $imagePath , $filename);
    $url['thumb'] = $url['images'][$sizes[0]];

    return $url;
}

private function resize($path , $sizes , $imagePath , $filename)
{
    $images['original'] = $imagePath . $filename;
    foreach ($sizes as $size) {
        $images[$size] = $imagePath . "{$size}_" . $filename;

        Image::make($path)->resize($size, null, function ($constraint) {
            $constraint->aspectRatio();
        })->save(public_path($images[$size]));
    }

    return $images;
}

I tried Change the code:

    $imagesUrl = $this->uploadImages($request->file('images'));
    return $imagesUrl;

It displaied return $imagesUrl well.

images  
300 "/upload/images/2018/300_tvto.jpg"
600 "/upload/images/2018/600_tvto.jpg"
900 "/upload/images/2018/900_tvto.jpg"
original    "/upload/images/2018/tvto.jpg"
thumb   "/upload/images/2018/300_tvto.jpg"

I think problem from array_merge

0 likes
10 replies
Cronix's avatar

Your problem isn't exactly the array_merge, it's what you're merging and then trying to save as the result of the array_merge.

$article = auth()->user()->article()->create(array_merge($request->all(), ['images' => $imagesUrl]));

$imagesUrl itself is an array, so the way you have it won't work.

Try checking your data, by testing things individually.

// merge data first, so you can see what it actually is
$data = array_merge($request->all(), ['images' => $imagesUrl]);

dd($data);
$article = auth()->user()->article()->create($data);

Hopefully by the result of that dd(), you'll see why that won't work when sending it to create().

mozew's avatar
Level 6

I get this message:

array:6 [▼
  "_token" => "ElHg1YiLLFAUkFqVWUdUV2qwZ2DxX5c1L5XcocYw"
  "title" => "Apple"
  "description" => "Apple is a company."
  "body" => "Apple Inc. is an American multinational technology company headquartered in Cupertino, California, that designs, develops, and sells consumer electronics"
  "category" => array:2 [▼
    0 => "1"
    1 => "2"
  ]
  "images" => array:2 [▼
    "images" => array:4 [▼
      "original" => "/upload/images/2018/1.jpg"
      300 => "/upload/images/2018/300_1.jpg"
      600 => "/upload/images/2018/600_1.jpg"
      900 => "/upload/images/2018/900_1.jpg"
    ]
    "thumb" => "/upload/images/2018/300_1.jpg"
  ]
]
Sergiu17's avatar
dd($file); // dumps the file, it exists.

$file = $file->move(public_path($imagePath) , $filename);

dd($file); // It can't find the file anymore, because it's moved to $imagePath

$sizes = ["300" , "600" , "900"];

$url['images'] = $this->resize($file->getRealPath() , $sizes , $imagePath , $filename);

$file->getRealPath() // won't work

Hope that this is the problem...yesterday I had similar problem :)

mozew's avatar
Level 6

I writted

dd($file); // dumps the file, it exists.

I get this message

UploadedFile {#205 ▼
  -test: false
  -originalName: "1.jpg"
  -mimeType: "image/jpeg"
  -size: 140203
  -error: 0
  #hashName: null
  path: "C:\xampp\tmp"
  filename: "phpA4F7.tmp"
  basename: "phpA4F7.tmp"
  pathname: "C:\xampp\tmp\phpA4F7.tmp"
  extension: "tmp"
  realPath: "C:\xampp\tmp\phpA4F7.tmp"
  aTime: 2018-07-09 16:31:54
  mTime: 2018-07-09 16:31:54
  cTime: 2018-07-09 16:31:54
  inode: 0
  size: 140203
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\xampp\tmp\phpA4F7.tmp"
}

What's the solution? thanks.

Sergiu17's avatar

First, solution is to try to read message till end.

$file = $file->move(public_path($imagePath) , $filename);

dd($file); // DO YOU GET SOMETHING HERE?
mozew's avatar
Level 6

I fill it out, but shows like the above.

protected function uploadImages($file)
{
    $year = Carbon::now()->year;
    $imagePath = "/upload/images/{$year}/";
    $filename = $file->getClientOriginalName();
    
    $file = $file->move(public_path($imagePath) , $filename);
    $file = $file->move(public_path($imagePath) , $filename);
    dd($file);
    
    $sizes = ["300" , "600" , "900"];
    $url['images'] = $this->resize($file->getRealPath() , $sizes , $imagePath , $filename);
    $url['thumb'] = $url['images'][$sizes[0]];

    return $url;
}
kadamv43's avatar

Hi, maybe this because php delete tmp file after moving file to new location and laravel still trying to access that file.

1 like

Please or to participate in this conversation.