image upload and save with multiple copies / names - php

How do I upload an images and then save with a different name and multiple copies in the same directory ?
One copy of the uploaded image(abcd.jpg) needs to be named '212_1_today_00.jpg' and another copy needs be named '424_1_today_00.jpg' and may be another '848_1_today_01.jpg'

better use time() instead of today as random no might be repeated at some point.......
<?php
if($_FILES){
$image = $_FILES['image'];
if($image['error'] == 0){
$a = explode('.',$image['name']);
$e = end($a);
$t = time();
$name = rand(100, 999)."_1_{$t}";
move_uploaded_file($image['tmp_name'], $name.'_00.'.$e);
# if u want 1 copy
copy($name.'_00.'.$e, rand(100, 999)."_1_{$t}_01.{$e}");
# if u want n more copies
/*
$n = 5; #no of copies u want
for ($i = 1; $i <= $n; $i++) {
copy($name.'_00.'.$e, rand(100, 999)."_1_{$t}_0{$i}.{$e}");
}
*/
echo 'Done';
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image" accept='image/*'/>
<input type="submit">
</form>

First I uploaded one with the name '212_1_today_00.jpg' and I copied using PHP function - copy
Found at http://www.php.net/manual/en/function.copy.php
<?php
$file = './myfolder/212_1_today_00.jpg';
$newfile = './myfolder/424_1_today_00.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}

Related

Check file exist on UploadiFive

I downloaded this upload example: https://github.com/xmissra/UploadiFive
When sending an apernas file, it works perfectly but when sending more than one file the function that checks if the file already exists presents a problem. Apparently the loop does not ignore that the file has just been sent and presents the message asking to replace the file. Is there any way to solve this?
This is the function that checks if the file already exists.
$targetFolder = 'uploads'; // Relative to the root and should match the upload folder in the uploader
script
if (file_exists($targetFolder . '/' . $_POST['filename'])) {
echo 1;
} else {
echo 0;
}
You can test the upload working at: http://inside.epizy.com/index.php
*Submit a file and then send more than one to test.
I tried it this way but it didn't work:
$targetFolder = 'uploads';
$files = array($_POST['filename']);
foreach($files as $file){
if (file_exists($targetFolder . '/' . $file)) {
echo 1;
} else {
echo 0;
}
When you have multiple files to be uploaded keep these things in mind;
HTML
Input name must be array name="file[]"
Include multiple keyword inside input tag.
eg; <input name="files[]" type="file" multiple="multiple" />
PHP
Use syntax $_FILES['inputName']['parameter'][index]
Look for empty files using array_filter()
$filesCount = count($_FILES['upload']['name']);
// Loop through each file
for ($i = 0; $i < $filesCount; $i++) {
// $files = array_filter($_FILES['upload']['name']); use if required
if (file_exists($targetFolder . '/' . $_FILES['upload']['name'][$i])) {
echo 1;
} else {
echo 0;
}
}

PHP Multiple File Uploader is uploading only a single file

I have been browsing through S.O for hours and still can't figure out why my code is not working. I am trying to upload multiple music files but for some reason only 1 file out of all selected is being uploaded.
Here's my code:
HTML
<input id="songs" name="songs[]" type="file" multiple="multiple" accept=".ogg,.flac,.mp3" />
PHP
$path = "../songs/"; //file to place within the server
$valid_formats1 = array("mp3", "ogg", "flac"); //list of file extention to be accepted
$tot = count($_FILES['songs']['name']);
for($i = 0; $i < $tot ; $i++){
$temporaryPathOfImage = $_FILES['songs']['tmp_name'][$i];
if ($temporaryPathOfImage != ""){
$dirPath = "../songs/" . $_FILES['songs']['name'][$i];
if(move_uploaded_file($temporaryPathOfImage, $dirPath)) {
echo "Stored in: " . "../songs/" . $_FILES['songs']['name'][$i];
}
}
}

Error in uploading files in yii2 move_upload function

Am doing multiple file upload in the controller but the file doesn't get uploaded
controller code: for the upload
$images = $_FILES['evidence'];
$success = null;
$paths= ['uploads'];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
echo $success;
}
// check and process based on successful status
if ($success === true) {
$evidence = new Evidence();
$evidence->case_ref=$id;
$evidence->saved_on=date("Y-m-d");
$evidence->save();
$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
I have tried var_dump($images['name'] and everything seems okay the move file does not upload the file
Check what you obtain in $_FILES and in $_POST and evaluate your logic by these result...
The PHP manual say this function return false when the filename is checked to ensure that the file designated by filename and is not a valid filename or the file can be moved for some reason.. Are you sure the filename generated is valid and/or can be mooved to destination?
this is the related php man php.net/manual/en/function.move-uploaded-file.php
Have you added enctype attribute to form tag?
For example:
<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

Renaming multiple files when uploaded

I have a form that uploads multiple file. The php code that I am using works fine but I would like to rename the files also and don't know how to go about this. I think adding a time stamp to the name would be the best answer. Here is the working code so far:
/* FILE UPLOAD CODE */
{
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploads/'; //set upload directory
/**
* we get a $_FILES['file'] array ,
* we procee this array while iterating with simple for loop
* you can check this array by print_r($_FILES['file']);
*/
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['file']['name'][$i];
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $_FILES['file']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
/* END FILE UPLOAD CODE */
Any help on what to add and where to accomplish this would be greatly appreciated.
If you want to give the same name to all the uploaded files, then you can get the name by using $_GET.
For example:
<form action="upload.php" method="post"> \\ this would send it to your page
<input type='text' size='30' name='filename'/>
<input type='submit' />
</form>
simply get the name using $_GET like this
$name = $_GET['filename'];
and now run your code but move the file with this name like this
move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $name[$i])
You can specify the new file name as the second parameter in move_uploaded_file()
For example:
move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . "new_file_name");

Storing file input ID inside variable during for loop | PHP

I am using the following HTML & PHP code to move multiple images to my server.
Is there a way using my code that I can associate a specific input element with the uploaded file?
So in my for loop, I can discover when the image from 'image_22' is being processed. Is this possible using my current code?
Dream world would be storing the value "image_22" inside a variable $imageNum :-)
Here is a snippet of what I'm currently working with...
HTML:
<input id="image_22" name="images[]" type="file" />
<input id="image_8" name="images[]" type="file" />
...
PHP:
<?php
if (isset($_POST['Submit'])) {
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
/**
* we get a $_FILES['images'] array ,
* we procee this array while iterating with simple for loop
* you can check this array by print_r($_FILES['images']);
*/
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory .
$_FILES['images']['name'][$i])) {
$number_of_moved_files++;
}
}
}
echo "Number of File fields created $number_of_file_fields.<br/> ";
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
}
?>
Thank you!!
In your HTML, just include that value inside the [].
<input id="image_22" name="images[22]" type="file" />
<input id="image_8" name="images[8]" type="file" />
In your loop, instead of an incremental for loop, use a foreach with index, which is a more common pattern in PHP than the incremental type. The $index will be the number supplied in the []:
// Loop over the ['name'] key in $_FILES['images'] to get all the named indexes
foreach ($_FILES['images']['name'] as $index => $filename) {
if ($filename != '') {
// not empty...
$number_of_uploaded_files++;
// Check for validity (see below)...
// Use the name concatenated with _$index to supply store the index with the filename
$uploaded_files[] = $filename . "_$index";
if (move_uploaded_file($_FILES['images']['tmp_name'][$index], $upload_directory . $filename . "_$index")) {
// successful rename
$number_of_moved_files++;
}
}
}
Note that your script is currently vulnerable to path injection attacks. You must filter the name of each file against the inclusion of things like ../ which could force the file to be saved anywhere on your filesystem (writable by the web server)! It is recommended to check the name with a regular expression of acceptable values:
// Verify that the uploaded filename contains only letters, numbers, hyphen, underscore, space before the `.` and letters only after the `.`
// You could also insist that it end in `.(jpg|gif|png)` or whatever your acceptable formats are
// Most important is to prevent things like `../`
if (preg_match('/^[a-z0-9_- ]+\.[a-z]+$/i', $filename)) {
// It's an ok filename
}

Categories