- Общие каталоги< /strong>:
Исходная папка и папка назначения являются общими сетевыми путями.
После загрузки файлов в папку назначения с новым имена, файлы также должны быть переименованы в исходной папке.
Моя проблема:
Однако я борюсь с:
- Переименование файлов в обоих исходных каталогах.
Код: Выделить всё
Image Upload with Rename Upload and Rename Images Preview Original Filename New Filename Actions Upload Close [img]${URL.createObjectURL(file)}[/img] ${file.name} Delete `; tableBody.append(fileRow); }); }); // Handle double click to preview image $(document).on('dblclick', '#imageTable tbody tr', function() { let imgSrc = $(this).find('img').attr('src'); $('#previewImage').attr('src', imgSrc); $('#previewModal').show(); }); // Close the image preview modal $('#closeModal').click(function() { $('#previewModal').hide(); }); // Handle delete row $(document).on('click', '.deleteRow', function() { $(this).closest('tr').remove(); }); // Upload images $('#uploadImages').click(function() { let formData = new FormData(); let files = $('#images')[0].files; // Loop through each file and add to FormData $('#imageTable tbody tr').each(function(index, row) { let newFilename = $(row).find('.newFilename').val(); if (newFilename === '') { // Remove row if new filename is empty $(row).remove(); } else { formData.append('images[]', files[index]); formData.append('newFilenames[]', newFilename); } }); // AJAX to upload images and rename $.ajax({ url: '/upload-images', method: 'POST', data: formData, processData: false, contentType: false, success: function(response) { alert('Images uploaded successfully!'); }, error: function(err) { alert('Error uploading images.'); } }); }); });
Код: Выделить всё
Route::post('/upload-images', 'ImageController@uploadImages');
Код: Выделить всё
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ImageController extends Controller
{
public function uploadImages(Request $request)
{
$images = $request->file('images');
$newFilenames = $request->input('newFilenames');
$sourcePath = '\\\\172.20.1.201\\source\\';
$destinationPath = '\\\\172.20.1.201\\destination\\';
foreach ($images as $index => $image) {
$originalName = $image->getClientOriginalName();
$newName = $newFilenames[$index] . '.' . $image->getClientOriginalExtension();
// Save to destination directory
$destinationFullPath = $destinationPath . $newName;
Storage::disk('network')->putFileAs($destinationPath, $image, $newName);
// Rename file in source folder
$sourceFullPath = $sourcePath . $originalName;
$newSourcePath = $sourcePath . $newName;
if (file_exists($sourceFullPath)) {
rename($sourceFullPath, $newSourcePath);
}
}
return response()->json(['message' => 'Images uploaded and renamed successfully']);
}
}
Код: Выделить всё
// Move the file to destination and remove from the source folder
if ($file->move($destinationPath, $newName)) {
if (File::exists($source)) {
File::delete($source); // Removing the file from the source directory
}
}
Процесс не может получить доступ к файлу, поскольку он используется другим процессом (код: 32 )
Мы используем HTML-форму загрузки файлов. Иногда мы выбираем файл из любой случайной папки, и в этот раз мы не будем переименовывать файл в исходный код. папка.
Подробнее здесь: https://stackoverflow.com/questions/790 ... -and-handl