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");
Related
I just started moving CodeIgniter 3 project to CodeIgniter 4.
Everything works fine except file upload.
I would like to keep the user uploaded files in /writable/uploads. Below is the code I use to move the uploaded file to desired location.
$target_dir = '/writable/uploads/recordings/';
$target_file = $target_dir . basename($_FILES["gfile"]["name"]);
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($FileType != "mp3") {
$vmuploadOk = 1;
}
else
$vmuploadOk = 1;
if ($vmuploadOk == 1) {
$greetfile = $id . "g" . basename($_FILES["gfile"]["name"]);
$target_filenew = $target_dir . $greetfile;
move_uploaded_file($_FILES["gfile"]["tmp_name"], $target_filenew);
}
I assume that it is because CI4 keeps writable folder outside public folder.
You are not using CodeIgniter's built in functions. Everything shown in your code are PHP functions. If you want to leverage built in CI functions, then look through the documentation as linked by #Boominathan Elango.
To get the file from the request:
$file = $this->request->getFile('here_goes_input_name');
As specified here
To move the file using CI function:
$file->move(WRITEPATH.'uploads', $newName);
As specified here
This worked for me and I hope it will also work for you. In codeigniter 4 please use this to upload your files and move it in your controller.
if($imagefile = $this->request->getFiles())
{
if($img = $imagefile['gfile'])
{
if ($img->isValid() && ! $img->hasMoved())
{
$newName = $img->getRandomName(); //This is if you want to change the file name to encrypted name
$img->move(WRITEPATH.'uploads', $newName);
// You can continue here to write a code to save the name to database
// db_connect() or model format
}
}
}
OR
if($img = $this->request->getFile('gfile'))
{
if ($img->isValid() && ! $img->hasMoved())
{
$newName = $img->getRandomName();
$img->move(ROOTPATH . 'public/uploads/images/users', $newName);
// You can continue here to write a code to save the name to database
// db_connect() or model format
}
}
Then in your html input field
<input type="file" name="gfile">
I hope this works else call my attention
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;
}
}
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];
}
}
}
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";
}
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
}