我在共享主机上的Laravel不上传新图像, 给我错误
图片来源不可读
一切在本地主机上运行良好。 我上传了该文件,并将公用文件夹中的所有内容都移到public_html,然后删除了公用文件夹。 如何删除与删除的公用文件夹相关的所有内容?
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\AdminController;
use Modules\Media\Helpers\FileHelper;
use Modules\Media\Models\MediaFile;
use Intervention\Image\ImageManagerStatic as Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
public function browser()
{
echo FileHelper::url();
}
public function sendError($message, $data = [])
{
$data['uploaded'] = 0;
$data['error'] = [
"message"=>$message
];
parent::sendError($message,$data);
}
public function sendSuccess($data = [], $message = '')
{
$data['uploaded'] = 1;
if(!empty($data['data']->file_name))
{
$data['fileName'] = $data['data']->file_name;
$data['url'] = FileHelper::url($data['data']->id,'full');
}
parent::sendSuccess($data, $message); // TODO: Change the autogenerated stub
}
public function compressAllImages(){
$files = MediaFile::get();
if(!empty($files))
{
foreach ($files as $file)
{
if(FileHelper::isImage($file))
{
if(Storage::disk('uploads')->exists('public/'.$file->file_path))
ImageOptimizer::optimize(public_path('app/public/'.$file->file_path));
}
}
}
echo "Processed: ".count($files);
}
public function store(Request $request)
{
$ckEditor = $request->query('ckeditor');
if (!$this->hasPermissionMedia()) {
$this->sendError('There is no permission upload');
}
$fileName = 'file';
if($ckEditor) $fileName = 'upload';
$file = $request->file($fileName);
$file_type = $request->file('type');
if (empty($file)) {
$this->sendError(__("Please select file"));
}
try {
static::validateFile($file, $file_type);
} catch (\Exception $exception) {
$this->sendError($exception->getMessage());
}
$folder = '';
$id = Auth::id();
if ($id) {
$folder .= sprintf('%04d', (int)$id / 1000) . '/' . $id . '/';
}
$folder = $folder . date('Y/m/d');
$newFileName = Str::slug(substr($file->getClientOriginalName(), 0, strrpos($file->getClientOriginalName(), '.')));
$i = 0;
do {
$newFileName2 = $newFileName . ($i ? $i : '');
$testPath = $folder . '/' . $newFileName2 . '.' . $file->getClientOriginalExtension();
$i++;
} while (Storage::disk('uploads')->exists($testPath));
$check = $file->storeAs( $folder, $newFileName2 . '.' . $file->getClientOriginalExtension(),'uploads');
// Try to compress Images
ImageOptimizer::optimize(public_path("uploads/".$check));
if ($check) {
try {
$fileObj = new MediaFile();
$fileObj->file_name = $newFileName2;
$fileObj->file_path = $check;
$fileObj->file_size = $file->getSize();
$fileObj->file_type = $file->getMimeType();
$fileObj->file_extension = $file->getClientOriginalExtension();
if (FileHelper::checkMimeIsImage($file->getMimeType())) {
list($width, $height, $type, $attr) = getimagesize(public_path("uploads/".$check));
$fileObj->file_width = $width;
$fileObj->file_height = $height;
}
$fileObj->save();
// Sizes use for uploaderAdapter:
// https://ckeditor.com/docs/ckeditor5/latest/framework/guides/deep-dive/upload-adapter.html#the-anatomy-of-the-adapter
$fileObj->sizes = [
'default' => asset('uploads/' . $fileObj->file_path),
'150' => FileHelper::url($fileObj->id, 'thumb'),
'600' => FileHelper::url($fileObj->id, 'medium'),
'1024' => FileHelper::url($fileObj->id, 'large'),
];
$this->sendSuccess(['data' => $fileObj]);
} catch (\Exception $exception) {
Storage::disk('uploads')->delete($check);
$this->sendError($exception->getMessage());
}
}
$this->sendError(__("Can not store the file"));
}