My PHP Script dont want to create a ZIP File - php

I tried to code a script, that put some data in a zip file. I think I have done all right, but he does not create the zip file.
I already tried a lot, but don´t find the issue.
<?php
$sql_abfrage_cloud = "SELECT * FROM dateien WHERE code = '$zugang' ORDER BY id";
$abfrage_cloud = $mysqli->query($sql_abfrage_cloud);
$verzeichnis = '/upload/';
$zip_name = date("dHis").'_fc.zip';
$anz_dateien = 0;
$error = 'fatal';
while($fetch = $abfrage_cloud->fetch_assoc()){
$anz_dateien = $anz_dateien + 1;
$zip_datei[$anz_dateien] = $fetch['path'];
}
$zip_arch = new ZipArchive;
$status = $zip_arch->open($zip_name, ZipArchive::CREATE);
if($status==true){
foreach($zip_datei as $datei){
$zip_arch->addFile($verzeichnis.$datei, $datei);
}
if(file_exists($zip_name)){
$error = 'false';
} else {
$error = 'true';
}
}
?>
I expected that $error would be 'false' but it´s 'true'.

You need to call $zip_arch->close() to save finish writing the file.
You should also use === when comparing the result of $zip_archive->open(), since the non-true results are numbers, and any non-zero number compares equal to true when type juggling is allowed.
$zip_arch = new ZipArchive;
$status = $zip_arch->open($zip_name, ZipArchive::CREATE);
if($status===true){
foreach($zip_datei as $datei){
$zip_arch->addFile($verzeichnis.$datei, $datei);
}
$zip_arch->close();
if(file_exists($zip_name)){
$error = 'false';
} else {
$error = 'true';
}
}

Related

ZipArchive::getStreamIndex kills script

I decided to try out the latest PHP ZIP functions for extracting data and something odd has happened. This PHP script is to read data from packed files of a specific extension (gob or goo) and display them in an XML format for further use later.
$zip = new ZipArchive;
$zipres = $zip->open($zipfilename);
if($zipres === true)
{
for($i = 0; $i < $zip->numFiles; $i++)
{
echo "\t<entry id=\"".$i."\">".$zip->getNameIndex($i)."</entry>\n";
if(strripos($zip->getNameIndex($i),".gob") !== false || strripos($zip->getNameIndex($i),".goo") !== false)
{
$zipentry = $zip->getStreamIndex($i);
return;
if($zipentry)
{
$packagenames[] = $zip->getNameIndex($i);
$wholeGOB[] = stream_get_contents($zipentry);
fclose($zipentry);
}
else
echo "\t<error>Error reading entry ".$zip->getNameIndex($i)."</error>";
}
}
$zip->close();
}
else
echo "\t<error>Invalid ZIP file</error>\n\t<code>".$zipres."</code>\n";
Having the return; before getStreamIndex will print out the <entry>s, but as soon as I move the return; to after getStreamIndex I get a blank page (even in view-source). Am I doing something wrong, or has this function always been a bit off? And what alternative would you recommend?

Check if Two Videos are the Same using php

I have search many time but i did not find any solution so in this case i can not post any code. sorry for this.
I have faced a problem that how can i check that 2 or more video same like i have media folder and many video upload in this folder then when i upload new video then need to check that video already exit or not.
1. if i have video demo.mp4 then when i will try to upload same video then give error
2. if i change video name like demo.mp4 to demo1.mp4 then i will give same error cause video name different but video content same
3. if i upload video demo5.mp4 then show me no error
i already checked image compare using
include('compareImages.php');
$new_image_name = $uploadfile_temp;
$compareMachine = new compareImages($new_image_name);
$image1Hash = $compareMachine->getHasString();
$files = glob("uploads/*.*");
$check_image_duplicate = 0;
for ($i = 0; $i < count($files); $i++) {
$filename = $files[$i];
$image2Hash = $compareMachine->hasStringImage($filename);
$diff = $compareMachine->compareHash($image2Hash);
if($diff < 10){
$check_image_duplicate = 1;
//unlink($new_image_name);
break;
}
}
but i can not compare video. someone help me
Tested and works fine, code taken from :http://php.net/manual/en/function.md5-file.php#94494 not mine.
<?php
define('READ_LEN', 4096);
if(files_identical('demo.mp4', 'demo1.mp4'))
echo 'files identical';
else
echo 'files not identical';
// pass two file names
// returns TRUE if files are the same, FALSE otherwise
function files_identical($fn1, $fn2) {
if(filetype($fn1) !== filetype($fn2))
return FALSE;
if(filesize($fn1) !== filesize($fn2))
return FALSE;
if(!$fp1 = fopen($fn1, 'rb'))
return FALSE;
if(!$fp2 = fopen($fn2, 'rb')) {
fclose($fp1);
return FALSE;
}
$same = TRUE;
while (!feof($fp1) and !feof($fp2))
if(fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) {
$same = FALSE;
break;
}
if(feof($fp1) !== feof($fp2))
$same = FALSE;
fclose($fp1);
fclose($fp2);
return $same;
}
?>

How to read a file line by line in php and compare it with an existing string?

I tried to write this program to compare a user-name in a file with an entered user-name to check whether it exists, but the program doesn't seem to work. Please help. The program was supposed to open a file called allusernames to compare the usernames. If the user name was not found, add it to the file.
<?php
$valid=1;
$username = $_POST["username"];
$listofusernames = fopen("allusernames.txt", "r") or die("Unable to open");
while(!feof($listofusernames)) {
$cmp = fgets($listofusernames);
$val = strcmp($cmp , $username);
if($val == 0) {
echo ("Choose another user name, the user name you have entered has already been chosen!");
$valid=0;
fclose($listofusernames);
break;
} else {
continue;
}
}
if($valid != 0) {
$finalusers = fopen("allusernames.txt", "a+");
fwrite($finalusers, $username.PHP_EOL);
fclose($finalusers);
?>
you need to replace linefeed/newline character from each line to compare.
while(!feof($listofusernames)) {
$cmp = fgets($listofusernames);
$cmp = str_replace(array("\r", "\n"), '',$cmp);
$val = strcmp($cmp , $username);
if($val == 0) {
echo ("Choose another user name, the user name you have entered has already been chosen!");
$valid=0;
fclose($listofusernames);
break;
} else {
continue;
}
}
i have added following line in you code
$cmp = str_replace(array("\r", "\n"), '',$cmp);
I havent tested this but I wonder if you could use something like
<?php
$user = $_POST["username"];
$contents = file_get_contents("allusernames.txt");
$usernames = explode("\n",$contents);
if(in_array($user,$usernames))
{
echo "Choose another username";
}
else
{
$contents .= "\n".$user;
file_put_contents("allusernames.txt",$contents);
}
I think things like file get contents etc. need a certain version of PHP but they do make things a lot nicer to work with.
This also assumes that your usernames are seperated by new lines.
Yo can do this more simple with this code:
<?php
$username = $_POST["username"];
$listofusernames = 'allusernames.txt';
$content = file($listofusernames);
if(in_array($username, $content)) {
echo ("Choose another user name, the user name you have entered has already been chosen!");
} else {
$content[] = $username . PHP_EOL;
file_put_contents($listofusernames, implode('', $content));
}
?>

PHP, Reading Text File For String

i am trying to read a text file and find out if there is a string in there. i have tried many different ways. this is what i have so far,
$file = "./userpass.txt";
$loginuser = $_POST[loginuser];
$loginpass = $_POST[loginpass];
$fileauth = file_get_contents($file);
if (strpos($file,$loginuser) !== false and strpos($file,$loginpass) !== false) {
echo 'Incorrect Password';
} else {
echo 'Hello The Master';
}
change
$loginuser = $_POST[loginuser];
$loginpass = $_POST[loginpass];
to
$loginuser = $_POST['loginuser'];
$loginpass = $_POST['loginpass'];
and this code reads file contents
$fileName = "newfile_testing.txt";
$file_handle= fopen($fileName , "r");
$theData = fread($file_handle, filesize($fileName));
print_r($theData);
You have syntax error. Fix it first. $_POST[loginuser]; should be $_POST['loginuser']; and $_POST['loginpass'];. In strpos() $file should be $fileauth.
$file = "./userpass.txt";
$loginuser = $_POST['loginuser'];
$loginpass = $_POST['loginpass'];
$fileauth = file_get_contents($file);
if (strpos($fileauth,$loginuser) !== false and strpos($fileauth,$loginpass) !== false) {
echo 'Incorrect Password';
}else {
echo 'Hello The Master';
}
Your main problem is that you are trying to find the user/password combination in the filename of that particular file ($file) instead searching against it's contents ($fileauth):
if (strpos($fileauth,$loginuser) !== false and strpos($fileauth,$loginpass) !== false) {
As noted by #Please Wait in his answer (nice catch), you also need to reference the $_POST indexes as a string, so
$loginuser = $_POST[loginuser];
$loginpass = $_POST[loginpass];
should be :
$loginuser = $_POST['loginuser'];
$loginpass = $_POST['loginpass'];

Actionscript 2.0, Having a SendAndLoad() function Issue

I have this code:
btn_jouer.onRelease = function ()
{
verif = txt_email_user.text;
if (txt_email_user.text == "")
{
txt_erreur.textColor = 16724736;
txt_erreur.text = "Champ(s) manquant(s)";
}
else if (verif.indexOf("#", 0) == -1 || verif.indexOf(".", 0) == -1)
{
txt_erreur.textColor = 16724736;
txt_erreur.text = "Adresse E-mail invalide";
}
else
{
php_login = new LoadVars();
php_login.email = txt_email_user.text;
php_login.sendAndLoad(_root.page_Login, php_login, "POST");
php_login.onLoad = function(succes)
{
if (succes)
{
//txt_erreur.text = php_login.etat;
//return;
if (php_login.etat == "exist")
{
_root.var_user.id = php_login.id;
_root.var_user.nom = php_login.nom;
_root.var_user.prenom = php_login.prenom;
_root.var_user.score = php_login.score;
_root.MovieLogin.unloadMovie();
if (_root._root.selectedPhone == "KS360")
{
_root.gotoAndStop(4);
}
else
{
_root.gotoAndStop(3);
} // end else if
}
else if (php_login.etat == "non")
{
trace (php_login.etat);
txt_erreur.text = "Email non enregistré! veuillez vous s'inscrir";
} // end if
} // end else if
};
} // end else if
};
The "page_Login" is login.php file on the server,
After debugging, the file login.php successfully received Posted data so i got:
$_POST['email'] = "what ever you type in swf form";
The login.php processor file:
if(isset($_REQUEST['email'])){
$email = strtolower(addslashes($_REQUEST['email']));
$DB->_request("select * from gamers where email='$email'");
if($DB->_nr() > 0) {
$row = mysql_fetch_array($DB->Result);
echo "&etat=exist&nom={$row['nom']}&prenom={$row['prenom']}&score={$row['score']}";
//
exit;
}
else {
echo "&etat=non";
exit;
}
}
Here above, the $DB->_nr() always returns "0" even the email address exists!
I have tried to create a simple html page having a form with method POST and have a simple input type text with a name="email"
When i write my email which is valid in the database and hit submit $DB->_nr() returns 1.
This really is driving me crazy, i'm sure that the email address exists, the login.php page receive posted data "email = validemail#domain.com" from SendAndLoad(); but mysql_num_rows returns 0.
Any one there had the same issue??
Any help would be so much appreciated!
Barry,
Use the following code in PHP to compare the email in both cases: given from flash and from HTML form:
if(isset($_REQUEST['email'])){
//createa the testFile.txt and give it attributes with 0777 for permission (in case you are under linux)
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, "-".$_REQUEST['email']."-\r\n");
fclose($fh);
$email = strtolower(addslashes($_REQUEST['email']));
$DB->_request("select * from gamers where email='$email'");
if($DB->_nr() > 0) {
$row = mysql_fetch_array($DB->Result);
echo "&etat=exist&nom={$row['nom']}&prenom={$row['prenom']}&score={$row['score']}";
//
exit;
}
else {
echo "&etat=non";
exit;
}
}
if you test for both of the cases, you will be able to compare the two exact forms. I have put "-" in the front and the end of it just to see if there are any whitespaces next to the email value.
Please reply with a compare result. thank you.

Categories