i need to change variable data if the 'data name' already exists..
i have this
if (file_exists('../../images/produtos/'.$name)){
$name = '1_'.$name;
}
it is working, if i have a file with the name that is saved in the variable $name, it create a new one with a 1_ before, but i need to check if there is a 1_.$name, 2_$name ...... until there is no file with the name saved in variable $name.
If i upload a file with name things.png i need to check if there is already a file called things.png, if there is then change a name to 1_things.png, but if there is a file called 1_things.png change the name to 2_things.png etc etc until there is no file with the same name.
PS: I dont want overwrite obv..
Im sorry for my bad english, but i really dont know how to explain this better, hope you guys understand.
Thanks in advance
Use a loop:
if(file_exists('../../images/produtos/'.$name)){
$i = 1;
while(file_exists('../../images/produtos/'.$i."_".$name)){
$i++;
}
$name = $i."_".$name;
}
Related
I am using the flash uploader that comes with Quform for wordpress to allow up to 5 files. I am trying to retrieve the file names but i only ever get the first one. $filesA All the files upload without issue but I never get all the names.
Here is my code (php) I am using
$myfiles= $form->getValue('iphorm_x_x');
$filesA = $myfiles[0]['text'];
$filesB = $myfiles[1]['text'];
$filesC = $myfiles[2]['text'];
$filesD = $myfiles[3]['text'];
$filesE = $myfiles[4]['text'];
I only ever get the first file name. Not sure why or where it is going wrong
Many thanks
Patrick
Found the answer after using Marc B to get existing file names
$myfiles= $form->getValue('iphorm_x_x');
foreach ($myfiles as $file) {
echo File Name: ' . $file['text'];
}
:)
I have to include a file stored in a database and retrieved as a variable.
how can I do it? I have tried this line but it doesn't work
include_once("News/$post->newspage");
the fild newspage contains the names of the files stored in News folder. for example his line works.
include_once("News/labourday2014.php");
you can try like
include_once("News/".$post->newspage);
include_once("News/{$post->newspage}"); should do the trick :)
Verify if the file exist then include it
if(is_file("News/".$post->newspage)) {
include_once("News/".$post->newspage);
} else {
// File dosent't exist
echo "File dosent't exist";
}
You can try this -
$fileName="News/".$post->newspage;
include_once($fileName);
My php script has to handle with non-slug filename uploads, when for example, a user tries to upload a file "Имя на русском.jpg". Some methods don't handle it correct, so i use:
$overwrite = true;
$slug = true;
\Web::instance()->receive(NULL,$overwrite,$slug);
So, I get the "imja-na-russkom.jpg" file on server. The problem is that I need to catch this filename for DB, for which I tried this:
$filename = \Web::instance()->Slug($_FILES['userfile']['name'];
Which returns "imja-na-russkom-jpg" - not the name used in filesystem (not dot, but dash before extension).
So, my question is: Is there a way to catch the name, used in Web::receive() or, if not, is there a workaround to get it by means of F3? Thank you.
Ok, with ikkez' tip i figured that out:
$a = Web::instance()->receive(NULL,true,true);
$name = array_keys($a);
$name = pathinfo($name['0'], PATHINFO_BASENAME);
Here we upload file on the server and save it's name into $name variable. You can skip the last line and use
$name = $name['0'];
if you want to save the full path to your uploaded file.
the \Web::instance()->receive function returns an array of files that where uploaded. you'll see the slugged name in the array keys. check the example at http://fatfreeframework.com/web#receive
What I want to do is add a number to the beginning of the file name so I don't have a duplicate file name in the folder.
So I choose my file "example.pdf" and upload, I got the the part of the code that looks like this:
move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
In this line of code above can I add a variable to change the file name to "1-example.pdf"?
Is that possible?
You can specify the new name as part of the second parameter of move_uploaded_file:
move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'][0], "$path1/example.pdf");
Hope this helps!
The move_uploaded_file method's second parameter is not only the path, but the filename to place the file. Try something like this:
move_uploaded_file($_FILES['ufile']['tmp_name'][0], $path1.'/[PUT NUMBER HERE]-'.$_FILES['ufile']['name'][0]);
What I did is that I created a random number which goes until to a million so the program will generate a number to rename a picture like this .though I did not use exactly your codes in your question .
#Rename images
$rand = rand(0,1000000);
$rename = $rand.$name;
$move = move_uploaded_file($temp_name,$target.$rename);
how do we attach mysql_insert_id() in a file uploads so that each file gets unique name ? I did try the following code but didn't work. please advice. Yhanks
$id = mysql_insert_id();
$dest = trim($uploaddir. $id. basename($_FILES['photo']['name']));
This should be
if(false !== ($id = mysql_insert_id())
{
$dest = sprintf("%s%d_%s",$uploaddir,$id,trim(basename($_FILES['photo']['name']));
if(!file_exists($dest))
{
//move_uploaded_file
}else
{
//You cant program :(
}
}else
{
//DB Error.
}
You should write your code so you add the file to the directory before you add to database.
Get the data from $_FILES
Sanitize the data and validate the file Size,Ext,Headers,Name
Make sure the file does not already exists
Generate a UniqueID and create a hash from a static string, Example 1
Store the file on the server and verify its move ok
Add meta data to the database along with the unique ID and file location
Bobs your uncle.
Example 1:
define('FILE_NAME_SALT','MySecret$alt');
$uid = md5(rand(0,100) . $uid . FILE_NAME_SALT . $FileName);
Store file like HASH.ext
This is a problem that you can take some steps to solve yourself before asking. For starters, print out $id. Is it a valid Id? Or is it FALSE (it might be). Then echo out $uploaddir, $_FILES['photo']['name'], and basename($_FILES['photo']['name']). Finally, echo out $dest. I'd be rather surprised if you don't catch the mistake by doing these simple debugging exercises.
You could use a hashing function to do this. For example, md5 or sha1 are good choices for unique filenames. You could use a filename that the md5() function returns given the file's initial name.