PHP Script fopen failed to open stream: > Invalid argument Error - php

I am getting a warning.
Warning: fopen(76561197992146126 .txt): failed to open stream:
Invalid argument in C:\wamp\www\Download\t3.php on line 6
For anyone wondering, this is the contents of main.txt below (http://pastebin.com/53chSRRz)
<?php
$APIkey = 'APIKeyHere';
$file = file('C:\wamp\www\Download\main.txt');
foreach ($file as $link) {
$link2 = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $link . "&format=json";
$downloaded = file_get_contents($link2);
$fh = fopen($link . ".txt", "a"); //or die("can't open file");
fwrite($fh, $downloaded);
}
echo "Finished";
?>
If I replace "fopen($link . ".txt", "a")" with a static file-name it works. But I need $link to be the filename. It is imperative to my setup.
I am running Windows 7 x64 using WAMP 2.2 with PHP 5.4.3

Your link IDs have whitespace in them. Try adding $link = trim($link); before creating $link2.

Related

Permission of CSV-file in PHP

My php file has multiple errors. The first one has to do something with permissions. Normally there's nothing wrong with it it, but now I use a csv-file. I think that's the problem. But I don't understand the other warnings.
Which permission do I have to add?
$filename = "menuitems.csv";
$fp = fopen($filename, "r");
while ($data = fgetcsv($fp, filesize($filename), ",")) {
if (trim($data[0]) == '') break;
$href = strtolower(substr($data[1], 0, 7));
if ($href == "http://")
print "<li> " . $data[0] . "</li>\n";
else
print "<li> <a href=\"" . data[1] . "\" >" . $data[0] . "</a></li>\n";
}
fclose($fp);
echo "</ul>\n";
echo "</div>\n";
Warning: fopen(menuitems.csv): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 10
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/basis/php/index.php on line 21
the csv file that was moved into the document root does not have permissions set correctly. You can do this several ways, including changing permission in the file properties dialog (via right click menu) or use the php command:
chmod("/somedir/somefile", 0644);
but installing xampp in its own folder off the root directory instead of Program Files directory will fix this issue in most XAMPP versions.

How do I create a file with PHP? (Ubuntu)

This may be marked as duplicate, but everything else I tried here didn't work. I am trying to create a file in PHP using this:
<?php
$con = $sup;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
fwrite($fp,$con);
fclose($fp);
?>
I am using ubuntu(xfce)
What is wrong with this?
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
is incorrect: normally $_SERVER['DOCUMENT_ROOT'] is already /var/www/. Try
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/HDdeltin/" . $sup , "wb");
or
$fp = fopen("/var/www/HDdeltin/" . $sup , "wb");
depending on whether you want your file under the document root or under a specific location.
<?php
$con = $sup;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
fwrite($fp,$con);
fclose($fp);
?>
I don't know if you can use the last parameter 'wb' for the fopen function. Try to use only 'w' option then let us know if it works.
This is what I came up with:
<?php
$text = "This will be placed in your file.";
$con = "test.txt";
$fp = fopen($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/$con", "wb");
fwrite($fp,$text);
fclose($fp);
?>
This seems to works if the folder HDdeltin exists, otherwise it will return an error.
$text would be the replacement of your $sub because you didn't declare one.
And I removed the /var/www/ part since this is exactly what $_SERVER['DOCUMENT_ROOT']; should return.

file_put_contents Failed to open stream: invalid argument

I have this code written in PHP, im using zend server on win 7 as localhost.
<?php
$dir = $_POST['link'];
// Create DOM from URL or file
require_once 'library/simple_html_dom.php';
$html = file_get_html($dir);
foreach($html->find('img') as $element){
$image = $element->src;
$imglink = $dir . $image;
$filename = $element->src;
$parsed_url = parse_url($filename, PHP_URL_PATH);
$basename = basename($filename).PHP_EOL;
$filenameOut = __DIR__ . "/images/" . $basename;
$pos = strpos($image, 'data:image');
if ($pos===false){
$parsing = parse_url($filename, PHP_URL_HOST);
if ($parsing != NULL){
file_put_contents($filenameOut, file_get_contents($image));
}
else{
file_put_contents($filenameOut, file_get_contents($imglink));
}
}
}
?>
And it gives me this error
file_put_contents(C:\Program Files (x86)\Zend\Apache2\htdocs/images/chrome-48.png ): failed to open stream: Invalid argument
Ive tried putting ways i've read from other questions from this site, but doesnt work.

PHP fopen() with command line

ANSWER I WENT WITH BELOW!
So I have a PHP script that works prefect via the web. I would like to set it up on a scheduled task on the server that is running the web hosting. It is a windows 2008 R2 server. I seems to run fine minus the fact it won't make the output files. Do I need to have the full path name? Ex C:\logs.... or will below work? I would like to keep the script working both in web and command line.
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen($File3, 'a+');
$Data3 = "blah";
fwrite($Handle3, $Data3);
more info:
here are the errors from the log file:
fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 686
PHP Warning: file_get_contents(bins/bins-10-09-2013.txt): failed to open stream: No such file or directory in C:\Websites\wordpress\win\import.php on line 692
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 699
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:\Websites\wordpress\win\import.php on line 709
WORKING!! Well, this is what I went with and it seems to work prefect.
$File = "" . $uploads_dir . "/import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($file1, 'a+');
if ( $Handle === false ) {
$File = "C:\Websites\wordpress\win\\".$uploads_dir."\import-" . date('m-d-Y-g-ia') . ".txt";
$Handle = fopen($File, 'a+');
}
This way I can Use the php script both via web and on a scheduled task via windows.
It seems fopen is failing to open the file . Make sure the file is successfully opened :
$Handle3 = fopen($File3, 'a+');
if( $Handle3 === false ) echo 'Unable to open file';
If it fails , check below items one at a time :
Check file / folder permissions .
Check with different slashes "/" , "\" in the path .
Check if it works with absolute path ( full path ) .
Check by changing directory before fopen : chdir('path\to\php\script')
etc .
If the issue is with the path , one way make it work both cases is :
// First try with the path that works from web
$File3 = "" . $log_dir . "/" . date('m-d-Y') . ".txt";
$Handle3 = fopen( $File3, 'a+' );
if( $Handle3 === false )
{
// It failed so may be scheduled task invoked the script
// So use the path that works in invoked by scheduled task
$File3 = "path\that\works\from\command\line";
$Handle3 = fopen( $File3 ,'a+' );
}
if( $Handle3 === false )
{
echo 'Failed to open file';
exit;
}
else
{
// What ever
}
I've never used the full url for an fopen or fwrite. These are the things I would look at first:
What is the output when you echo $log_dir?
Does the directory exist?
Are permissions set correctly?
If you create the file before running the script, does it then successfully write to the file?
What is the value of $Handle3 after the fopen?
What is the return value of the fwrite that you call on the last line?
Is error handling set to all? (it should be for debugging)

Giving PHP permission to access COM port

I'm creating a php script that connects to a 3G modem connected via serial connection on COM5.
I'm getting the following error and I believe it is because php does not have r/w access to COM5:
Warning: fopen(COM5:) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\SMStest\test2.php on line 9
// mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off
$fp = fopen ("COM5:", "w+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
$e = chr(27);
$string = $e . "A" . $e . "H300";
$string .= $e . "V100" . $e . "XL1SATO";
$string .= $e . "Q1" . $e . "Z";
echo $string;
fputs ($fp, $string );
fclose ($fp);
}
There are many ways to access COM ports on windows, alternatives to your method are opening it with the following paths:
\Device\00000123 (You can find the correct value in device manager, properties, details, physical device object name)
\\.\com5 (This is how I would open the port as a file if I was writing a program in C or something)

Categories