PHP - Fopen Not Opening A File In A Different Folder =\
if (!file_exists('../images/flags/imNum.txt')) {
$file1 = fopen('../images/flags/imNum.txt','c'); fclose($file1); } why won't that work =\ it makes no sense to me Similar Tutorialshi, I want to write pdf file but it is getting garbage value i check following url http://bugs.php.net/bug.php?id=21904&edit=1 Code: [Select] $fp = fopen($fdirandname,'wb'); fwrite($fp,$pdfcode); fclose($fp); I have a script that I am trying to transfer one file from a remote server to my local server and save it. The files can be quite large. I have successfully backed up around 200 MB... But, anything over that seems to fail with a strange error... I was hoping for a little guidance... here is my code and error/warning that I get: PHP Code: function remote_capture($tmp_url, $filename) { $r_handle = fopen($tmp_url, "rb"); $d_handle = fopen($filename, 'w'); if($r_handle&&$d_handle) { while(($buffer = fgets($r_handle)) !== false) { fputs($d_handle, $buffer); } fclose($r_handle); fclose($d_handle); return true; } else { return false; } } Warning Message: Warning: file_get_contents(URL_OF_MY_SCRIPT_HAS_BEEN_REMOTED) [function.file-get-contents]: failed to open stream: HTTP request failed! in /path/on/my/local/server/to/script on line 298 So in my last thread I was working on checking if a file was there...I resorted to something not in the library and used $pic_exists_jpg = fopen($jpg_path,'r'); $pic_exists_jpeg = fopen($jpeg_path,'r'); $pic_exists_gif = fopen($gif_path,'r'); $pic_exists_png = fopen($png_path,'r'); if ($pic_exists_jpg != '0') { $jpg_test = '1'; } elseif ($pic_exists_jpg == '1') { $jpg_test = '2'; } elseif ($pic_exists_jpg == '0') { $jpg_test = '2'; } elseif ($pic_exists_jpeg != '0') { $jpeg_test = '1'; } elseif ($pic_exists_gif != '0') { $gif_test = '1'; } elseif ($pic_exists_png != '0') { $png_test = '1'; } now for some reason php is being a poo and no matter what returning that there is a file there. no matter what method i use be it fopen or file_exists or anything else, it saying I have a file in that directory BUT I DO NOT! THERE IS NOT ONE OF THOSE FILES. I know I have my paths right because If I echo them directly I get the picture...what the heck?! Hey guys, I have a script that is run once a week to fetch some user info for a large number of accounts. The code I have now writes the info correctly, but I was wondering if there was an easy way to have the accountdata.txt file completely wiped before new updated info is written to it, as opposed to deleting it manually every week. $FileName = "accountdata.txt"; $f=fopen($FileName,"a+"); fwrite($f, $UserKey[$i]." ".$SigKey[$i]."\n"); $f=fclose; I could in theory just open the file and delete everything in it manually, save it and then run this script, but I'm always looking for more ways to make things efficient, and come a time when it's run more than once a week it'd be nice to have all that automated. So in summary, I need it to delete all data (if any) from the accountdata.txt file before it writes anything to it. Thanks. Can't figure out why the page wont save after clicking save. $KOHDE="index.php?s=admin"; $LOMAKE_W = 400; $TEXTAREA_W = 50; $TEXTAREA_H = 20; $LOMAKE = mysql_real_escape_string($_GET["l"]); $LOMAKE2 = mysql_real_escape_string($_POST["lomake"]); after a few more lines... elseif ($LOMAKE2 == "etusivu"){ #Etusivun muokkaus $teksti = stripslashes($_POST["teksti"]); $f = fopen("etusivu.php", "w"); fwrite($f, $teksti); fclose($f); } elseif ($LOMAKE == "etusivu"){ #Etusivun muokkaus echo "<h1>Edit home page</h1>"; $f = file("etusivu.php"); echo "<form action='$KOHDE' method='post'> <textarea name='teksti' cols='$TEXTAREA_W' rows='$TEXTAREA_H'>"; for ($i = 0; $i < count($f); $i++){ $f[$i] = str_replace("\"", "''", $f[$i]); echo $f[$i]; } echo "</textarea> <input type='hidden' value='etusivu' name='lomake'/> <br><input type='submit' value='Save'/> </form><br/>"; html_ohje(); } I can't see where it has gone wrong. Can anyone help? I have a page using forms to help build listing templates for eBay. I have a folder where I have hundreds of logos stored. I know the logo names but not their extensions. . . . I have to test each potential (jpg, jpeg, gif, png, etc.) until I guess right. Here is an example code for the web form:
<form action="extension_test2.php" method="post"> <p>Logo: <input name="e" value="" type="text" size="15" maxlength="30" /><p> <input name="Submit" type="Submit"/> </form> and the form's result: <? $e =$_POST['e']; if(!empty($e)) { echo '<img src="http://www.gbamedica...ebayimg/logos/'.$e.'">'; }; ?> Here is a link to the example: http://www.gbamedica...ension_test.php Use "olympus.jpg" for test. I am looking for code that can determine the file type and dynamically add the extension. Can it be done? Hey, I'm trying to open a .php file and read a comment block that is in the beginning of the file. I'm having some trouble in that nothing is being echoed out. Here is the reading file: <?php $fb = fopen("tepl.php", "r"); $data = fread($fb, 8192); fclose($fb); var_dump($data); ?> here is the file (tepl.php) being read: <?php /* Name: Test Plugin 1 URI: http://www.theorangeday.com/plugintest/ Description: This is just a test plugin because I need to test how this thing is going to work overall. Author: Thomas Francis Author URI: http://www.theorangeday.com/ Version: 0.1 */ function test_plugin(){ echo "we loaded the test plug"; } ?> the var_dump returns Code: [Select] string(321) " " What's wrong here? Hello, I'm working on a script which should read lines from a file keyword.txt create a new file with that variable name, insert the content close and loop through all the lines in the keyword file. The loop itself works but now that i'm adding the fopen fwrite and fclose i'm running into trouble. I'm not that great a coder and have pieced this together so far. I'd appreciate any suggestions on how to fix the fopen and other file functions to create unique files using the keywords from the file Thanks in advance <?php $read_file = file('keywords.txt'); foreach($read_file as $var) { $file = fopen($var, "x"); $content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>This is the title</title> </head> <body> <h2>The keyword is $var</h2> <p><p> </body> </html>"; fwrite($file, $content); fclose($file); } ?> I have a php script on a server writing to an html file on a remote server. I'm using the following combination of fopen and fputs. The problem is that once out of maybe every 10 or 15 writes to the file, the file that is written is blank with nothing in it. Has anyone here had experience with this before? The file is fixed the next time it writes to it. this code is within a loop that runs every 10 seconds. Code: [Select] // Allows overwriting of existing files on the remote FTP server $stream_options = array('ftp' => array('overwrite' => true)); // Creates a stream context resource with the defined options $stream_context = stream_context_create($stream_options); // Opens the file for writing and truncates it to zero length if ($fh = fopen($ftp_path, 'w',0, $stream_context)) { if ( $change > 0 ) fputs($fh, "the contents of the html file is here"); //the case if change is positive else fputs($fh, "the contents of the html file is here"); //the case if change is negative // Closes the file handle fclose($fh); } else { die('Could not open file.'); } Hey guys! I have the following doubt: I have a php file that does the following alongside with other code not displayed he $fp = fopen("emails.txt", "w"); It opens a txt file called emails.txt that is in the same folder where the php is saved but how do I tell $fp = fopen("/folderbefore/emails.txt", "w"); ?? In other words, how do I write the path on the script that the emails.txt is located in a previous folder where the php file is located?? Thanks in advance for your help Cheers! Hey all, I'm just having a problem with this code.. the local server says it has an error on a particular line that will be pointed out in the coding section. Code: [Select] <?php $hd = fopen('content.txt', 'r'); $line = fgets($hd); $content = array(); while($line) { $word = explode(',', $line); $content [$word[0]] => array('img_path' => $word[1], 'description' => $word[2], 'price' => $word[3]); // the error message is indicating this line is broken!! Is it because of an equal sign missing? I tried putting an equal sign, but still it doesn't run. $line = fgets($hd); } fclose($hd); ?> Your help is greatly needed.. Hi. I am working with some rather large files. Sometimes I am just opening a file and trying to find an entry on a single line. Sometimes I am opening a file and working on each line.
Whats the most efficient way to work with a large file?
If I open a file, and place all lines into an array, I will use a lot of memory.
If I open a file, and work on it line by line, I should use much less memory, but will the process take longer?
Whats most efficient?
I have underlined my main question above.
Thanks :-)
I have the following code:
$fp = fopen(“path_to_file”, ‘a’); flock($fp, LOCK_EX); fwrite($fp, $string); flock($fp, LOCK_UN); fclose($fp);If I try to lock the file in two different places at the same time, this will cause a race condition. How can I prevent this? I know in Java, for example, it has a concurrent library which contains reentrant lock, which basically tries to get the lock and if can't waits. What can I do in PHP? Hi, I have a form where a user selects a file to attach to the email. At the moment when you select a file it uploads from the user device. How do i change this so that a user can attach a file from a folder on the server. For example the folder name is uploadinvoice so when the user selects the browse button to attach a file it opens up the uploadinvoice folder on the server so the user can select the file from there ?
Thanks
coding i have at moment function ValidateEmail($email) { $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i'; return preg_match($pattern, $email); } if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'form1') { $mailto = $_POST['youremail']; $mailfrom = isset($_POST['myemail']) ? $_POST['myemail'] : $mailto; $subject = 'Message'; $message = 'Message'; $success_url = './test.php'; $error_url = ''; $eol = "\n"; $error = ''; $internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response"); $boundary = md5(uniqid(time())); $header = 'From: '.$mailfrom.$eol; $header .= 'Reply-To: '.$mailfrom.$eol; $header .= 'MIME-Version: 1.0'.$eol; $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol; $header .= 'X-Mailer: PHP v'.phpversion().$eol; try { if (!ValidateEmail($mailfrom)) { $error .= "The specified email address (" . $mailfrom . ") is invalid!\n<br>"; throw new Exception($error); } $message .= $eol; foreach ($_POST as $key => $value) { if (!in_array(strtolower($key), $internalfields)) { if (!is_array($value)) { $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol; } else { $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol; } } } $body = 'This is a multi-part message in MIME format.'.$eol.$eol; $body .= '--'.$boundary.$eol; $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol; $body .= 'Content-Transfer-Encoding: 8bit'.$eol; $body .= $eol.stripslashes($message).$eol; if (!empty($_FILES)) { foreach ($_FILES as $key => $value) { if ($_FILES[$key]['error'] == 0) { $body .= '--'.$boundary.$eol; $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol; $body .= 'Content-Transfer-Encoding: base64'.$eol; $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol; $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol; } } } $body .= '--'.$boundary.'--'.$eol; if ($mailto != '') { mail($mailto, $subject, $body, $header); } header('Location: '.$success_url); } catch (Exception $e) { $errorcode = file_get_contents($error_url); $replace = "##error##"; $errorcode = str_replace($replace, $e->getMessage(), $errorcode); echo $errorcode; } exit;
} The root directory:
header.php
stylesheet.css
In the following example I am trying to include the header.php file in a sub folder.
When I include the header.php like in the following example then the stylesheet.css file will not work anymo
<?php include("../header.php"); ?>The stylesheet.css file is included in the head tags of the header.php file. Is the above example the right way to do it? If yes, how can I do it so the stylesheet.css file will work too. Hello, I'm trying to have my index.php to open/run another test.php file. I'm having my own server that I play with, that I run Ubuntu on. So the index.php are located at /var/www/ directory, but I want to run a file that are located at /testing/test.php The final test.php is file for showing pictures, and I don't want to out all the pictures under the /var/www/ location. It's alot of photos. I don't know much about php but I have been trying this: <?php header("Location: /var/www/testing/test.php"); //These below are desperat old tries. //header("Location: ./testing/test.php"); //header("Location: ../testing/test.php"); //$handle = fopen("/privat/Web_pictures/test.php", "r"); //"/testing/test.php" // header("Location: ./test.php"); //This one actually works, but I'm still in the wrong folder (/var/www/) echo "test "; // NN4 requires that we output something... exit(); ?> Thankful for help! I'm trying to extract the contents of a zip file to a folder. I found the ZipArchive class and followed the examples to get it to work for the most part. But I want to extract the files in the folder inside the zip file but leave the folder out. So it should extract just the files to my given destination. I found this on php.net. Code: [Select] If you want to copy one file at a time and remove the folder name that is stored in the ZIP file, so you don't have to create directories from the ZIP itself, then use this snippet (basically collapses the ZIP file into one Folder). <?php $path = 'zipfile.zip' $zip = new ZipArchive; if ($zip->open($path) === true) { for($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $fileinfo = pathinfo($filename); copy("zip://".$path."#".$filename, "/your/new/destination/".$fileinfo['basename']); } $zip->close(); } ?> For some reason that 'copy' line is not working for me. Obviosly I've changed the variables in the line to the correct variables. Can someone help me out. Thanks Mike Hello all, i am go9090go. Today i made a domains for a jar file people can upload from my website. I made this to make the jar file close source and its easy to update. Now i made a java classloader and everything i made works. The classloader call a php document with the password and username. The pass and name will be checked inside a databse and if its inside i use header() to load the jar file. But when i just go to my main domain i get the index of the site and people can easly download the jar file without have to walk thru the php pass checker. So i want to place the jar file inside a protected folder,and i want that only way you get acces to this jar is by the php file. How can i get a file from a protected folder? here is the php used when the jar file is not inside a protected folder: <?php $DBName = "name";//name database $DBUser = "name";//user $DBPassword = "pass"; //passs $DBHost = "host"; //might be different mysql_connect($DBHost, $DBUser, $DBPassword); mysql_select_db($DBName); $username = $_GET['username']; $password = $_GET['password']; $IP = $_SERVER['REMOTE_ADDR']; $string = "Java"; $pos = strpos($agent, $string); if (!strpos($_SERVER['HTTP_USER_AGENT'], "Java")) { echo("Your Auth has been banned for trying to breach security."); //mysql_query("delete from users where username='$username'"); exit(); } $query = "select * from users where name='$username' and pass='$password'"; mysql_query($query); $num = mysql_affected_rows(); if ($num > 0) { header('Location:script/Script.jar'); } ?> now i want to use the header to a file inside a folder that is protected : so how can i make the header() methode to open script.jar inside a protected folder. The folder haves name and pass: blabla,balbla for exempel thanks for help Hi all, i want to download a file from the server but instead of storing it in the downloads i want it to store it directly in the folder i want and i also dont want to show any download window that appears while we download any file. Friend please help..... Hi everyone, I'm still learning, but getting intermediate in PHP now, but it is still challenge to learn. I'm trying to have php check to see if one file inside folder in server, seem I could not get it right, but I tested it on other site, it works, but not this script, I don't understand why it won't work...maybe logical is wrong? here my code: if ($_POST['video']) { $path1 = "UPLOADS/Home/"; $path2 = "UPLOADS/Breakfast/"; $path3 = "UPLOADS/Spider/"; $scan1 = scandir($path1); $scan2 = scandir($path2); $scan3 = scandir($path3); $count1 = count($scan1) - 3; $count2 = count($scan2) - 3; $count3 = count($scan3) - 3; if($count1 > 0) { header('location:exist.html'); }elseif ($count2 > 0) { header('location:exist.html'); }elseif ($count3 > 0) { header('location:exist.html'); } But other site, it works: $scan = scandir($path); $count = count($scan) - 3; echo $count; if($count > 1){ echo "Hello yourself!<br />"; } Anyone will help will be appreciate! Thank you! Gary Edited April 3, 2019 by sigmahokies |