repeated warnings while trying to upload images - php

Warning: fopen() expects parameter 1 to be string, array given in /home/speedycm/public_html/speedyautos/carphoto.php on line 42
Warning: filesize() [function.filesize]: stat failed for Array in /home/speedycm/public_html/speedyautos/carphoto.php on line 43
Warning: fread(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 43
Warning: fclose(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 44
i keep getting these error messages whenever trying to upload a picture on my website and i'm not sure how to sort them out. can anyone please help? lines 36-59 read:
$CarInfo->Load();
if ($hidaction == "addphoto")
{
$ctrP = 0;
foreach ($_FILES['pics'] as $pics)
{
if ($_FILES['pics']['name'][$ctrP] <> "")
{
if (is_uploaded_file($_FILES['pics']['tmp_name'][$ctrP]) or die("No Image: " . $_FILES['pics']['name'][$ctrP]))
{
$ext = substr(strrchr($_FILES['pics']['name'][$ctrP], "."), 1);
$fp = fopen($_FILES["pics"]["tmp_name"], 'rb');
$contents = fread($fp, filesize($_FILES["pics"]["tmp_name"]));
fclose($fp);
if (preg_match("/system/", $contents) OR preg_match("/<\?/", $contents))
{
$error .= "Invalid image: {$_FILES['pics']['name'][$ctrP]}<br />";
//$pieces = explode(".", $_FILES['pics']['name'][$ctrP]);
//$ext = $pieces[count($pieces) - 1];
} elseif ((in_array($ext, $types_array)) AND ($_FILES['pics']["size"][$ctrP] < (MAXFILE_SIZE * 1000000)))
{
$orgImageName = "cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);
$thmImageName = "thumb_cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);
$dtlImageName = "dtl_cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);
many thanks in advance and sorry for the wild indentation btw :-)

I'm not a PHP guy, but it looks to me as if _FILES is a three-dimensional array of strings. Sometimes you use three indexes:
$_FILES['pics']['tmp_name'][$ctrP]
But in the fopen() call, you only use two; this means you're passing a 1D array of strings to fopen(), which is wrong. You need a third index on the array on this line:
$fp = fopen($_FILES["pics"]["tmp_name"], 'rb');

You're forgetting to properly reference the multiple images loaded into the $_FILES array you uploaded in the lines that are causing errors.
Where you have:
$fp = fopen($_FILES["pics"]["tmp_name"], 'rb');
$contents = fread($fp, filesize($_FILES["pics"]["tmp_name"]));
Should be this:
$fp = fopen($_FILES["pics"]["tmp_name"][$ctrP], 'rb');
$contents = fread($fp, filesize($_FILES["pics"]["tmp_name"][$ctrP]));
What you're sending is an array ($_FILES["pics"]["tmp_name"]) into a function that expects a string, which should be the value plucked from the array ($_FILES["pics"]["tmp_name"][$ctrP]).

Related

fopen says failed to open stream: Undefined error: 0 but the fileexist is passes

I am trying to find the WAV file duration using the following code.
if ( !file_exists($location_wmv.$name_wmv) ) {
echo "<BR> File Does not Exist<BR>";
} else {
echo "<BR> File Exist<BR>";
}
$file = $location_wmv.$name_wmv;
$fp = fopen($file, ‘r’);
$size_in_bytes = filesize($file);
fseek($fp, 20);
$rawheader = fread($fp, 16);
$header = unpack(‘vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits’,
$rawheader);
$sec = ceil($size_in_bytes/$header['bytespersec']);
$duration_wmv = $sec;
echo "<BR> Raw Suration.". $duration_wmv . "<BR>";
$duration_wmv = gmdate("H:i:s", $demo_song_duration_sec);
echo "<BR>WAV Duration".$duration_wmv;
Here the file_exists function says that file Exist. But the fopen says failed to open stream: Undefined error: 0
The file is actually present in the folder. But still I get this error.
This:
$fp = fopen($file, ‘r’);
looks iffy. Are those backticks?
Maybe try:
$fp = fopen($file, 'r');
(simple quotes)

PHP Having problems with find and replace in LARGE CSV

I am writing a PHP script so that I can do a find and replace in a large CSV file. I wrote this script:
// FIND AND REPLACE
$sourcePath = 'custom.csv';
$tempPath = $sourcePath . 'temp';
$source = fopen($sourcePath, 'r');
$target = fopen($tempPath, 'w');
while(!feof($source)) {
$line = preg_replace ("village", "village/",fgets($source));
fwrite($target, $line);
}
fclose($source);
fclose($target);
unlink($sourcePath);
rename($tempPath, $sourcePath);
But I am getting these errors,
Warning: feof() expects parameter 1 to be resource, boolean given
Warning: fgets() expects parameter 1 to be resource, boolean given
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
$source = fopen($sourcePath, 'r'); isn't returning what you think it is.
It's likely returning false, which typically happens when PHP can't find the file at the path you provided. If you're certain the file exists, you should confirm that the user executing the script has the proper permissions to read the file.
You're second issue regarding preg_replace() is being caused by not using delimiters. They are needed in the first argument.
$line = preg_replace ("/village/", "village/",fgets($source));
However, regular expressions aren't needed with this simple of a replacement. You should instead use str_replace() and the script should run faster.
Your code should look like this:
<?php
$sourcePath = 'custom.csv';
$tempPath = $sourcePath . 'temp';
$source = fopen($sourcePath, 'r');
$target = fopen($tempPath, 'w');
if($source){
while(!feof($source)) {
$line = str_replace("Village\\", "Village",fgets($source));
fwrite($target, $line);
}
} else {
echo "$sourcePath not found, or using the wrong permissions.";
}
fclose($source);
fclose($target);
unlink($sourcePath);
rename($tempPath, $sourcePath);
?>
You are not checking if fopen is actually returning a file pointer resource or a false result. It is likely returning false and throwing the warning that a boolean is provided.
Also, you could use:
$line = str_replace("village", "village/", fgets($source));

fwrite(): supplied argument is not a valid stream resource

I keep getting 3 errors when I use this code:
Warning: fopen() [function.fopen]: Filename cannot be empty
Warning: fwrite(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource
I don't know what to do. I'm a php noob.
<?php
$random = rand(1, 9999999999);
$location = "saves/".$random;
while (file_exists($location)) {
$random = rand(1, 999999999999);
$location = "saves/".$random;
}
$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
As per your original question before your edit:
Since the file doesn't exist yet, your while condition won't work and that's why you're getting those error messages.
And since you're using a random number for the file, you will never know which file to open in the first place. Just remove the while loop.
Try this:
<?php
$random = rand(1, 999999999999);
$location = "saves/".$random;
$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
From the code you have, it looks like $location only exists inside the scope of the while loop. Try
<?php
$location = "";
while (file_exists($location)) {
$random = rand(1, 999999999999);
$location = "saves/".$random;
}
$content = "some text here";
$fp = fopen($location,"wb");
fwrite($fp,$content);
fclose($fp);
?>
first of all, you must set values to your $location variable or since the file is not yet created try this:
$random = rand(1, 999999999999);
$location = "saves/".$random;
$content = "some text here";
//if(file_exists($location)) $fp = fopen($location,"wb");
$fp = fopen($location, 'wb') or die('Cannot open file: '.$location); //implicitly creates file
fwrite($fp,$content);
fclose($fp);

feof(): 3 is not a valid stream resource in

after i have installed windows 8 on my desktop and reinstalled aptana and xampp, i somehow can't use !feof($handle). i want to get the symbols of nasdaq stored in my $symb arra.here is an example and my error:
$symb = array();
$url = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
$handle = fopen("$url","r");
while( !feof($handle) ){
$line = fgetcsv($handle, 1024);
if($line!="Symbol" && isset($line[0]) && $line[0] != null ){
$symb[] = trim($line[0]);
}
fclose($handle);
}
And my Errors :
Warning: feof(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 61
Warning: fgetcsv(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 62
Warning: fclose(): 3 is not a valid stream resource in C:\xampp\htdocs\demos\screener\candleScreener.php on line 66
.......
Is there a setting i have to change on the php.ini file or what could it be ?
thanks.
.....
$url = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";
$handle = fopen("$url","r");
$txt = fread( $handle, 8 );
print_r($txt);
.....
prints out :
"Symbol"
so my fopen() is fine ....
The reinstallation and the fopen() are red herrings. You're closing the file handle inside the while loop, before the file has been read.
while( !feof($handle) ){
$line = fgetcsv($handle, 1024);
if($line!="Symbol" && isset($line[0]) && $line[0] != null ){
$symb[] = trim($line[0]);
}
// fclose($handle); // Move this outside the while loop
}
fclose($handle); // Moved this outside the while loop
I had the same problem in my code. I had an extra file close function in the code. In other words I was trying to write to the file for a second time and it was not open for use. I had this coded in the top of my program:
$newFile = $local_directory . $tenant.'-'.$namespace.'_Stats.xml';
$newDoc = fopen($newFile, "w");
fwrite($newDoc, $urlStats);
fwrite($newDoc, $response);
fclose($newDoc);
Then later I had:
fwrite($newDoc, $response);
fclose($newDoc);
Check to make sure the file is open before you write more content.

accessing files named in an array

I am a new PHP developer and I just started working with files in PHP.
I have the following code to count number of txt files in the directory and store their names in an array and then using a loop display the total lines in each of the files!
here is the code, help me where I have gone wrong!
$dir = opendir('directory/');
$num_files = 0;
$dir_files[] = array();
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..','Thumbs.db')) and !is_dir($file)){
$num_files++;
echo $file;
array_push($dir_files,$file);
echo "<br />";
}
}
echo "--------------------------------------<br />";
echo "Number of files in this directory: ".$num_files."<br />";
echo "--------------------------------------<br />";
foreach($dir_files as $dir_file=>$value){
$file='directory/'.$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
I get the following errors:
Notice: Array to string conversion in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 19
Warning: fopen(directory/Array): failed to open stream: No such file or directory in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 21
Warning: feof() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\PHP_practice\read_lines_of_files.php on line 22
Your code is toooooooo lengthy. Try this : This will do whole functionality for you, let me know if any issues.
foreach(glob('directory/*.txt',GLOB_BRACE) as $value){
$file =$value;
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo "File $file has $linecount lines!";
}
change:
$dir_files[] = array();
to
$dir_files = array();
And:
fopen() returns a file pointer resource on success, or FALSE on error.As it is throwing an error opening the file, feof() is receiving FALSE instead of a file pointer resource: so you get the error "expects parameter 1 to be resource, boolean given in"...

Categories