Using a PHP script, I want to compare two images. One of the images is located on my server, and one is located on an external website. I have tried to compare the hashes of the two images to each other. Unfortunately, this only works when the two images are saved on my server. How can I make this work?
<?php
$localimage = sha1_file('image.jpg');
$imagelink = file_get_contents('http://www.google.com/image.jpg');
$ext_image = sha1_file($imagelink);
if($localimage == $ext_image){
//Do something
}
?>
If you are using php 5.1+ (which I hope) you can just write :
<?php
$localimage = sha1_file('image.jpg');
$ext_image = sha1_file('http://www.google.com/image.jpg');
if($localimage == $ext_image){
//Do something
}
?>
As sha1_file will work on remote wrappers.
Quote from PHP doc at https://secure.php.net/manual/en/function.sha1-file.php
5.1.0 Changed the function to use the streams API. It means that you can use it with wrappers, like sha1_file('http://example.com/..')
You are not using the sha1_file() properly in the second call.
sha1_file() expects the parameter to be a filename and you are using a memory buffer. So you have 2 options.
First using your current code, save the file to a temp location and use sha1_file()
<?php
$localimage = sha1_file('image.jpg');
$imagelink = file_get_contents('http://www.google.com/image.jpg');
file_put_contents('temp.jpg', $imagelink);
$ext_image = sha1_file('temp.jpg');
if($localimage == $ext_image){
//Do something
}
?>
Or use sha1() instead of sha1_file() on the contents of $imagelink
<?php
$localimage = sha1_file('image.jpg');
$imagelink = file_get_contents('http://www.google.com/image.jpg');
$ext_image = sha1($imagelink);
if($localimage == $ext_image){
//Do something
}
?>
Well actually maybe 3 options, see #Flunch's answer!
Related
I've already asked a couple of questions regarding this and each step gets me closer however it still doesnt work as intended.
I want to upload an image and write it to the textfile, then when i upload another image that will be written to the end and so on so forth. So ultimately you'll have a long file with lots of images.
As far as i can tell my code should work but it doesn't. Here is a link to the site website for testing. Testing it maybe useful and below is the code.
It also always creates an empty element at the end of the array as you'll see from testing the site.
The PHP:
$sFileName = "imgDB.txt";
for ($i=0 ; $i < count($_FILES) ; $i++) {
move_uploaded_file(
$_FILES['file-'.$i]['tmp_name'],
"img/". $_FILES['file-'.$i]['name']
);
}
$sImgs = file_get_contents($sFileName); //gets a string from the file.
if (json_decode($sImgs, true) != false) {
$ajImgs = json_decode($sImgs, true);
} else {
$ajImgs = array();
}
$aOutPut = array_merge ($ajImgs, $_FILES);
$aSendToFile = json_encode(
$aOutPut,
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
);
file_put_contents($sFileName, $aSendToFile);
Some remarks
if the move_uploaded_file call is not protected further, this allows to upload any files, including script files - this opens a security vulnerability in your application - check for MIME-type and file-extension to avoid this
json_decode returns null if the input value is empty or cannot be decoded - not false like in your code
appending to an array is not done with array_merge, this just overrides the properties of $_FILES of the previous execution - use $aOutPut[] = $_FILES; instead
I get a ressource stream when I use fopen on a filepath I get from my database :
<?php
$query = "SELECT filepath
FROM files
WHERE id=123";
$result = do_query($query);
/*do_query() is a personal simplification*/
$line = mysql_fetch_array($result);
$file = fopen($line['filepath'], 'r');
/*var_dump on file return "resource(51) of type (stream)"*/
?>
But I my project I need a $_FILE type... Can I convert a ressource stream in $_FILE type?
$_FILES is not a "type", it's merely an array which holds certain information about uploaded files. First of all, if you have some piece of code which is hardcoded to use $_FILES, you should probably change it to accept a generic argument instead. Meaning, instead of:
function foo() {
echo $_FILES['tmp_name']:
}
Rewrite that to:
function foo($path) {
echo $path;
}
You can then call that function and pass it any path from anywhere. In your case you'd pass it $line['filepath'] as is. You don't need to fopen it, because then you get a resource, when you currently just want a path.
If you need to "fake" the $_FILES array, you need to construct it manually:
$_FILES['foo']['name'] = '..';
$_FILES['foo']['type'] = '..';
$_FILES['foo']['size'] = '..';
$_FILES['foo']['tmp_name'] = $line['filepath'];
$_FILES['foo']['error'] = UPLOAD_ERR_OK;
But again, you'll probably want to alter whatever code is hardcoded to use $_FILES instead of this hackaround.
I have an array, which contains amongst other things a file path to a video file. There are various file formats .avi, .mp4 etc etc.
Now I only want to display the rows where the file path's file extension is .mp4.
Currently I'm using a function to extract the file extension, but I'm having trouble doing this on the fly while displaying the rows.
An example of the file path: c:\TempVideoDir\Office-P-cam01-20140826-083016.avi
The function im using to get the file ext:
function get_fileext($filename) {
$cut_fileext = (explode('.', $filename));
$just_fileext = $cut_fileext[1];
echo $just_fileext;
}
Which works fine.
Now I'm querying the database to get the filepath along with other details about the file, and I only want to display the rows where the file path contains a .mp4 file.
I've tried everything I could thing of/find on Google etc but this is what I currently have:
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
if (in_array((get_fileext($row["Filename"])== "mp4"), $result)) {
echo " blah blah
I dont have the option of adding the file extension into the database table as this is backed up automatically by a piece of equipment.
So, whadoyareckon? is it possible to do it on the fly like this?
Any help is much appreciated!
Thanks
I dont think you can use in_array() like that. Its searching the entire index of the array, so when it looks ate $result[] its never going to find $row['filename'] with only an ext.
try this:
//looping through $result array
foreach($result as $row) {
//check if $row['filename'] has a string with a mp4 extension.
if (get_fileext($row["Filename"])== "mp4") {
echo "Row has a filename with the mp4 extension';
also as someone else pointed out fix your get_fileext() method to return something.
function get_fileext($filename) {
$cut_fileext = (explode('.', $filename));
$just_fileext = $cut_fileext[1];
return $just_fileext;
}
Your function isn't returning a value. It's emitting the value to the output:
function get_fileext($filename) {
$cut_fileext = (explode('.', $filename));
$just_fileext = $cut_fileext[1];
echo $just_fileext;
}
So there's no way for anything invoking that function to see that result. Instead, return the value:
function get_fileext($filename) {
$cut_fileext = (explode('.', $filename));
$just_fileext = $cut_fileext[1];
return $just_fileext;
}
That way the invocation of the function itself evaluates to the returned value, allowing you to use the function in your logic.
You could consider adjusting your database query to only return rows with ".mp4" files.
Change your query to have something like:
filepath = "%.mp4"
In MySQL the % operator is a wildcard, so this will match any strings ending in ".mp4"
so the title is not full clear, my question , I'm using the code to rename the file from directory present in the server the problem is i have to use the HTML form and php to update the file name, i want to do this : there will be an option on every file for renaming it when i click on the option the box pops up and i have to type the new name for file and save it , any help will be appreciated. (before down voting think about the question.)
The code that I'm using to update the file name
<?php
include("configuration.php");
$target = $_POST['filename'];
$newName = $_POST['newfilename'];
$actfoler = $_REQUEST['folder'];
$file = "files/users/";
$new ="files/users/";
$renameResult = rename($file, $new);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $file . " is now named " . $new;
} else {
echo "Could not rename that file";
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Try changing these lines:
$file = "uploads/$loggedInUser->username$actfolder/$target";
$new ="uploads/$loggedInUser->username$actfolder/$newName";
To:
$file = "uploads/{$loggedInUser->username}{$actfolder}/{$target}";
$new ="uploads/{$loggedInUser->username}{$actfolder}/{$newName}";
To explain why:
You are using variables inside a string, which means you will want to tell PHP where the variable ends. Especially when referencing objects or arrays, but also when you are placing variables right next to each other. I'm guessing PHP evaluated your original line to uploads/[Object]->usernamePizza/newname
I don't think you can call object properties in a string as you do.
try replace these lines :
$file = "uploads/".$loggedInUser->username."$actfolder/$target";
$new ="uploads/".$loggedInUser->username."$actfolder/$newName";
You may think about echoing $file and $new to confirm the path is nicely built.
On a side note, I'd recommend to really check the entries because this code can obviously lead to major security issues.
As the title said i need a way to set the variable name depending of what the name of the picture is (i got over 100 different pictures)
Since i got custom classes in another php file for each picture (like tags) like for example:
$picture1 = "hillside sunset";
$picture2 = "beach cyprus";
and so on, so i need to fetch each variable for each picture
Heres the current loop where the div class is going to be each pictures name ($PICTURENAME is just to define where this code goes and is irelevant codewise):
<?php
foreach (glob("img/*.jpg") as $filename)
{
$path = $filename;
$file = basename($path);
$file = basename($path, ".jpg");
echo '<div class="'.$PICTURENAME.'" id="'.$file.'"><img src="'.$filename.'"> '.$file.' </div>';
}
?>
Don't use 100+ variables. Using a database would make far more sense, but if you don't want to get into learning that (you should, though), using a data structure would still make far more sense.
You could create one array (and use it as a map), and have the filename as the key, and the value would be the tags.
In PHP, you can address a variable using another variable:
$name = "foo";
${$name} = "bar";
echo $foo; // prints "bar"
echo ${$name}; // the same as above
However, as Kitsune already recommended, you are better off using something else, e.g., an array.