I have an application where i have to upload files through a loop. It uploads the first file but it gives an error that the file "" does not exist. Following is the array where I am getting files.
and following is the trace for the error.
Any idea what am I missing or what could be the problem?
UPDATE:
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
Finally fixed it. Here is my previous controller action which was not working.
public function done(Request $request){
$packages = ['instagram', 'facebook', 'twitter'];
foreach($packages as $package){
// this is the line which was creating the problem
$form_data = $request['contents'][$package];
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
}
}
To fix the problem just assign the request variable to another variable. This is because the request processes the files in the first loop and then it cannot find the files for other attributes in the next iteration.
public function done(Request $request){
$packages = ['instagram', 'facebook', 'twitter'];
// Fixed it with this line
$contents = $request['contents'];
foreach($packages as $package){
$form_data = $contents[$package];
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
}
}
Related
I need to add manually files into $_FILES, then I use this method :
public function addToFiles($key, $filename): void {
$tempName = tempnam('/tmp', 'php_files');
$originalName = basename(parse_url($filename, PHP_URL_PATH));
$rawData = file_get_contents($filename);
file_put_contents($tempName, $rawData);
$_FILES[$key]['name'][] = $originalName;
$_FILES[$key]['type'][] = mime_content_type($tempName);
$_FILES[$key]['tmp_name'][] = $tempName;
$_FILES[$key]['error'][] = 0;
$_FILES[$key]['size'][] = strlen($rawData);
}
I see temporary file in temporary directory but move_uploaded_file () return false.
What wrong with this code ?
Thank you
I'm having a problem in my code, I am trying to append a number to a filename if filename already exists. It goes something like this
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
while (file_exists($location.$fileName)) {
$i++;
}
$fileName= $i.$fileName;
$name = $fileName;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);
if ($moveResult != true) {
#unlink($fileTmpLoc);
header('location: ' . URL . '?page=0&sort=name&type=desc&folder=uploads/&message=uploaderror');
}
Unfortunately for some reason $i wont increase its value by 1 every time it loops, instead it adds to the filename this way 1234filename.jpg my file name variable is after the loop and i cant understand why this is accruing. I am expecting to get ($i)filename.jpg a single number
AFTER RESTARTING MY LOCALSERVER IT STARTED WORKING WITH THE CODE PROVIDED BELOW DUUUH
You need to use the actual filename when you concat the number to it and not the one you already added a number to.
// not sure why you are splitting the filname up here
$explode = explode(".", $fileName);
$extension = end($explode);
$fileactualname = reset($explode);
$i = 0;
$fn = $fileName;
while (file_exists($location.$fn)) {
$i++;
// add number to actual filename
$fn = $i.$fileName;
}
$name = $fn;
$moveResult = move_uploaded_file($fileTmpLoc, $location . "/". $name);
I am trying to upload multiple images to my server, however it is not working. It gives me status code 200 but it doesn't upload images.
This is what I do:
if($request->hasFile('post_image')){
$images = $request->post_image;
$i = 0;
foreach($images as $image){
$i++;
$filename = $post->id.'.'.$i.'jpg';
$location = '/var/www/site/html/'.$post->id;
$image->move($location, $filename);
}
}
If I am removing the foreach() then it uploads image but only one.
Why it is not uploading them? What I am doing wrong?
Edit:
Answering my own question here also. The problem was that my key "post_image" had to be "post_image[]" instead.
You forgot to add . before 'jpg' extension in your filename:
Should be like this:
$filename = $post->id.'.'.$i.'.jpg';
I am trying this way upload multiple images:
if(!empty(Input::file('image'))){
$files= Input::file('image');
$destinationPath= 'images';
$images=array();
foreach($files as $file){
$fullname= $file->getClientOriginalName();
$hashname = $fullname;
$upload_success =$file->move($destinationPath, $hashname);
$images[]=$fullname;
$has= implode(",",$images);
}
$categories = new CategoryType;
$categories->name = Input::get('name');
$categories->image_attachment = $has;
$categories->save();
}
Follow this awnser you can loop on the all uploaded files
Get all files in a foreach loop in Laravel
like $images = Input::file('post_image');
The problem was that my key "post_image" had to be "post_image[]" instead.
$file_ary = array();
$file_count = count($request->file('image') );
$a=($request->file('image'));
$finalArray=array();
for ($i=0; $i<$file_count; $i++) {
$fileName = time().$a[$i]->getClientOriginalName();
$destinationPath = $request->input('path') ;
$finalArray[$i]['image']=$destinationPath.$fileName;
$a[$i]->move($destinationPath,$fileName);
}
return json_encode($finalArray);
I'm creating an upload form that when uploading files will check to see if a file with the same name already exists in the directory. If a file with the same name is found, the script should increment the file by appending the file name with a number such as test1.txt test2.txt and so on until a match is not found. This is what I have come up with so far but wanted to see if there is a different approach I should take.
Also note, my filenames are already cleaned before they enter this part of the script so my filename and extension functions are simplified.
function filename($file){
return substr($file,0,strrpos($file,'.'));
}
function extension($file){
return strtolower(substr(strrchr($file,'.'),1));
}
$new_file = 'test.txt';
$dir = 'files';
$files = scandir($dir);
$exclude = array('.','..');
foreach($files as $file){
if(!in_array($file,$exclude)){
$file_array[] = $file;
}
}
$i = 1;
while(in_array($new_file,$file_array)){
$filename = filename($new_file);
$extension = extension($new_file);
if($i>1){
$num = strlen($filename);
$filename = substr($filename,0,-1);
$new_file = $filename.$i.'.'.$extension;
} else {
$new_file = $filename.$i.'.'.$extension;
}
echo $new_file.'<br>';
echo $i.'<br>';
$i++;
}
echo $new_file;
i am new to php, i am trying to upload a file to a url, where php script should have the logic to check if file already exisits, it should append a value to the file name and then check again if it exisits, if not then proceed further to save it.
Here is the while loop:
$target_path = $target_path . basename( $_FILES['userfile']['name']);
$i = 1;
while (true) {
if (file_exists($target_path)) {
$target_path = $i . "_" . $target_path;
$i++;
} else {
break;
}
}
Now, im not sure if break; works the same as in other languages. What i am trying to do is the target path if already exists, will be updated and checked at each iteration of while loop, if it does not exists, the else should break the while loop, and later i will save the file with that name.
It works only if the file does not exisits, after that it just fail to rename the file path.
What is wrong here?
Edited, this should work.
$i = 1;
$base = basename( $_FILES['userfile']['name']);
while (file_exists($target_path.$i.'_'.$base)) {
$i++;
}
$target_path = $target_path.$i.'_'.$base;
Perhaps this little piece of code will help you:
function safeFilename($image_path,$filename_original)
{ $extension = strrchr($filename_original, ".");
$filename_base = substr($filename_original,0,-1*strlen($extension));
$filename = $filename_base;
$counter = 1;
while(file_exists($image_path.$filename.$extension))
{ $filename = $filename_base . $counter;
$counter++;
}
return $filename.$extension;
}
Update: my bad, fixed, thanks for your feedback.