Hi all,
When I initially began with laravel, I didn't know what I was doing, I have a bunch of files that are stored on my server and I need to move them to a digital ocean space.
there are 2 things with this;
- I stored them in a not great way so, i have file names with dates and names.
- Upon trying to move them I keep getting errors that I cannot seem to track down.
I will post the code below and some images to show how they are stored, again not great
the context to this is I have a list of companies, who may or may not have images stored against either what is called a ticket or what is called a project. At some point someone on the team changed the way the images are stored so there are multiple columns in the database I need to look at.
Once I get the image I need to find out its ticket or project id and store it in a path on my do space that goes company_id/tickets (or projects)/ticket_id or project_id and finally the image name.
everything for my connection to Digital ocean is set properly, I have new uploads going there, I just need to move the old items there as well. I keep encountering the error below. However, everything with the file Object seems to be correct.
I am using Laravel 9 and vuejs
CODE:
"<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Company;
use App\Models\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\UploadedFile;
class MoveImagesToDo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'movefilestoDo';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uploadPath = "/testing";
$oldImageId = [];
$companies = Company::select('id')
->get();
$foudCounter=0;
$notFoudCounter=0;
foreach ($companies as $c) {
dump('begin');
$ticketImages = image::where('ticket_id', '>', 0)
->where('company_id', $c->id)
->get();
foreach ($ticketImages as $ti) {
dump("inforeach1");
//check if the file is there
if (file_exists(storage_path().'/app/images/'.$ti['Image_title'])) {
dump("file there [check1]");
// $myImgTile = $ti->Image_title ?? $ti->Image;
//$test = Storage::disk('do')->allDirectories($c->id . '/tickets/' . $ti['id']);
$test = Storage::disk('do')->allDirectories($uploadPath."/".$c->id . '/tickets/' . $ti['id']);
if (!$test) {
// $testDir = Storage::disk('do')->makeDirectory($c->id . '/tickets/' . $ti->id);
$testDir = Storage::disk('do')->makeDirectory($uploadPath."/".$c->id . '/tickets/' . $ti['id']);
}
$compId = $ti->company_id;
$fileName = $ti['Image_title'];
$fileP = Storage::path($ti['Image_title']);
$fileSize = 2000; //Storage::size($ti['Image_title']);
$mimeType = Storage::mimeType($ti['Image_title']);
$_fileObj = new UploadedFile($fileP, $fileName, $mimeType, $fileSize, true);
$filePath = $uploadPath;
$path = $this->uploadFileToDO(rtrim($filePath, '/'), $fileName, $_fileObj);
array_push($oldImageId, $ti->id);
$foudCounter ++;
}
}
}
dump($foudCounter);
dump($notFoudCounter);
}
public static function uploadFileToDO($filePath, $fileName, $fileObj, $public = true)
{
dump("Failing here?");
dump("$fileObj");
dump($fileObj->path);
$isPublic = $public ? 'public' : '';
$stored = Storage::disk('do')->putFileAs($filePath, $fileObj, $fileName, $isPublic);
return $stored;
}
}"
FILE OBJECT WITH ERROR:
"begin" // app\Console\Commands\MoveImagesToDo.php:43
"inforeach1" // app\Console\Commands\MoveImagesToDo.php:50
"file there [check1]" // app\Console\Commands\MoveImagesToDo.php:53
"Failing here?" // app\Console\Commands\MoveImagesToDo.php:88
Illuminate\Http\UploadedFile^ {#2984 // app\Console\Commands\MoveImagesToDo.php:89
-test: true
-originalName: "1683034205_abbottstown.png"
-mimeType: "application/octet-stream"
-error: 2000
#hashName: null
path: "D:\Apps\Client_care_system\storage\app"
filename: "1683034205_abbottstown.png"
basename: "1683034205_abbottstown.png"
pathname: "D:\Apps\Client_care_system\storage\app\1683034205_abbottstown.png"
extension: "png"
realPath: false
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
ValueError
Path cannot be empty
at D:\Apps\Client_care_system\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:397
393▕ * @return string|false
394▕ */
395▕ public function putFileAs($path, $file, $name, $options = [])
396▕ {
➜ 397▕ $stream = fopen(is_string($file) ? $file : $file->getRealPath(), 'r');
398▕
399▕ // Next, we will format the path of the file and store the file using a stream since
400▕ // they provide better performance than alternatives. Once we write the file this
401▕ // stream will get closed automatically by us so the developer doesn't have to.
1 D:\Apps\Client_care_system\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:397
fopen()
2 D:\Apps\Client_care_system\app\Console\Commands\MoveImagesToDo.php:95
Illuminate\Filesystem\FilesystemAdapter::putFileAs()
I will add code below -- thanks in advance !!!!!
Error Text:
"begin" // app\Console\Commands\MoveImagesToDo.php:45
"inforeach1" // app\Console\Commands\MoveImagesToDo.php:52
"file there [check1]" // app\Console\Commands\MoveImagesToDo.php:55
"Failing here?" // app\Console\Commands\MoveImagesToDo.php:91
"D:\Apps\Client_care_system\storage\app\1683034205_abbottstown.png" // app\Console\Commands\MoveImagesToDo.php:92
ErrorException
Undefined property: Illuminate\Http\UploadedFile::$path
at D:\Apps\Client_care_system\app\Console\Commands\MoveImagesToDo.php:93
89▕ public static function uploadFileToDO($filePath, $fileName, $fileObj, $public = true)
90▕ {
91▕ dump("Failing here?");
92▕ dump("$fileObj");
➜ 93▕ dump($fileObj->path);
94▕
95▕
96▕ $isPublic = $public ? 'public' : '';
97▕ $stored = Storage::disk('do')->putFileAs($filePath, $fileObj, $fileName, $isPublic);
1 D:\Apps\Client_care_system\app\Console\Commands\MoveImagesToDo.php:93
Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap{closure}()
2 D:\Apps\Client_care_system\app\Console\Commands\MoveImagesToDo.php:73
App\Console\Commands\MoveImagesToDo::uploadFileToDO()
PS D:\Apps\Client_care_system>