How to use wildcard in php - php

I'm trying to rename a file but I'm having a hard time changing it.
Basically I have a file named scorpion.jpg. What I want is change scorpion.jpg to default.jpg
scorpion.jpg is a uploaded file so it can be any name. This is what I have so far.
rename('path_to_image/' . '*.jpg', 'path_to_image/' . 'default.jpg');
Any help is greatly appreciated.

You should try exec('mv "'.$path.'*.jpg" "'.$path.'".default.jpg');
exec function manual
Hope it helped !
EDIT 1 :
So, to stay full PHP i would do this
$scan = glob($path.'*.jpg');
if(!empty($scan)){
if( !rename($path.$scan[0], $path.$newName)){
echo 'Failed rename';
}
else{
echo 'Renamed!';
}
}

Related

PHP search video files in directory and display single video

I am Justin and new to Stackoverflow. We have a yearly familyweekend and every year someone else creates the entertainment. This year it is my turn. I am creating a game (treasure hunt) with the use of an iPad.
For this I am looking for a script that can search for a filename in a directory and when found, display the file (video) in the page. I am a little familiar with PHP, but not very.
It does not require any database it can be very simple because it will only contain a few videos. But I can't find any scripts that quite do what I need. Every script is either to complicated and advanced or does not show the video but the filestring.
Can anyone help me by pointing me in the right direction? Thanks so much in advance.
Regards,
Justin
to check if a specific file exists in a directory you can use file_exists() method.
Please check the documentation: http://php.net/manual/en/function.file-exists.php
So after trying and trying, I think I found the answer. Below the script for anyone looking for this.
<?php
$dir = 'directoryname';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['formname']))? strtolower($_GET['formname']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
echo "<video width='320' height='240' controls>";
echo "<source src='$dir/$file'>$file type='video/mp4'>";
echo "</video>";
echo "";
echo "<br>";
}
}
closedir($res);
?>
And the form
<form action="search.php" method="get"><input name="formname"
type="text"> <input type="submit"></form>

How to while loop images from directory

Surprisingly, Google had nothing.
My old method of uploading images was storing the actual image in the database. I'm trying to change my method to storing the photos in directories. But I'm having troubles figuring out how to display them to the users. I'm trying to while loop the files. Much help is greatly appreciated. Also, PDO is not my strong suit, trying to learn it the best I can. Here is what I have.
$query6 = "SELECT * FROM photos WHERE userID=$memberID";
$result6 = $db->query($query6);
while ($row6 = $result6->fetch(PDO::FETCH_ASSOC)) {
$photo = $row6['photo'];
while ($photo == true) {
echo "<img src=\"images/$memberID/$photo\">";
}
}
The page isn't loading at all. Not even an error. Your help is greatly appreciated. Thank You!
The code does insert the photo name in database and properly upload the photo to the directories. I only need assistance with displaying to the user. :)
while ($photo == true)
{?>
<img src="images/<? echo $memberID;?>/<?echo $photo;?>">
close your img tag.like
echo "<img src=\"images/$memberID/$photo\" />";

php echo filenames in a directory?

I am using the following php code to echo out the names (and extensions) of all the files in a folder.
This works fine and I am producing the results in a table like so:
code:
<?php
$dir = new DirectoryIterator("data/uploads/");
foreach ($dir as $fileinfo) {
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}?>
result:
. Existing File Delete
.. Existing File Delete
Filename1.jp Existing File Delete
Filename2.jp Existing File Delete
Filename3.jp Existing File Delete
The problem is in my results I am also getting those weird dots at the top. I do not know why these are being shown as I only have 3 files in the directory and don't know where the dots have come from?
Can anyone please help with this? thanks
Add Condition to check that is it file or not.. try this code
if ($fileinfo->isFile()) { // add this condition
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}
Add condition and check if it is . or .. if it satisfies the condition continue your looop

How to get the link of current directory in php?

I have been trying to get the current directory link and come up with something like this as:
<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]paypal_success.php"; ?>
but really that didn't work as I got the output as :
http://localhost/php-login-new/invoice.phppaypal_success.php
so really I want to trim the invoice.php from it so if you can guide me please how I can do that..it will be great..!
Try to use dirname()
<?php
echo "http://$_SERVER[HTTP_HOST]".dirname($_SERVER[REQUEST_URI])."/paypal_success.php";
?>
$path = test/invoice.phppaypal_success.php
you should use basename($path), result is invoice.phppaypal_success.php
or you can use basename($path,".html"), result is invoice.phppaypal_success
$result_path = basename($path)
then you can use substr() in php
echo substr($result_path,5,-22);
Maybe this can help you

php to display file information and download link if files are present - How to adjust for multiple files

I have a piece of php that if a specified folder has a file in, it will display the filename, date created and present a download button, if the folder is empty it will show nothing. This works very well but if I have more than one file in the folder it bunches all the filenames together - what I want is the separate information displayed for every file.
To help you understand the problem here is an image showing the problem and the code. I got very far on my own but its way above my head, I just cant see a simple way to correct the problem. The code may look very awkward and odd as I'm totally new at this but it looks visually right on the browser. I would really appreciate any help thank you.
Here is an image of the problem: http://i46.tinypic.com/m79cvs.png
<?php if (!empty($thelist)) { ?>
<p class="style12"><u>Fix</u></p>
<p class="style12"><?=$thelist?><?php echo " - " ?> <?php $filename = '../../customers/client1/client1.fix.exe';
if (file_exists($filename)) {
echo "" . date ("m/d/Y", filemtime($filename));
}
?> <?php echo " - <a href='download.php?f=client1/client1.fix.exe'><b>Download</b></a> <a href='download.php?f=client1/client1.fix.exe'>
<img src='../css/images/dlico.png' alt='download' width='35' height='32' align='absmiddle' /></a>" ?>
</p>
<?php } ?>
The list ($thelist) contains your files, yes?
You are not working on the $thelist, but on the $filename which is a hardcoded string.
Why? Currently you are outputting <?=$thelist?> and it looks like concatenated string from filenames. I would suggest that $thelist should be something like an array of your files. Then you could iterate over the files and output html dynamically for each entry.
<?php
// define your directory here
$directory = xy;
// fetches all executable files in that directory and loop over each
foreach(glob($directory.'/*.exe') as $file) {
// output each name and mtime
echo $file . '-' . date ("m/d/Y", filemtime($file));
// or you might also build links dynamically
// $directory needs to be added here
echo ''.$file.' - Size: '.filesize($file).'';
}
?>

Categories