Jump to content

[SOLVED] quick code question


soycharliente

Recommended Posts

I'm getting these 3 errors for the 3 lines after...

 

Warning: fopen(upload/upload_log/2007_07_16.txt): failed to open stream: No such file or directory in main.php on line 526

Warning: fwrite(): supplied argument is not a valid stream resource in main.php on line 527

Warning: fclose(): supplied argument is not a valid stream resource in main.php on line 528

 

<?php
$fp = fopen($log,"a+");
fwrite($fp,"$ip | $file_name | $screen_size | $date | $time");
fclose($fp);
?>

 

The entire file can be seen at http://www.phpfreaks.com/quickcode/File-Uploader/152.php around line 215.

Yes, the path in the comments at the top of the script is different than what I use (I didn't like it). I just copied all the example code into a test.php file just to see if it would work and I get those errors for those lines.

Link to comment
Share on other sites

try running the following code right before you open the stream (right before $fp=fopen($log,"a+")):

 

if(is_file($log))
   echo "File Exists";
else
   echo " File does not exist";

 

If this returns "File does not exist" then the path to the file probably has an error, otherwise if it says "File Exists" let me know and i'll look for other methods to troubleshoot

Link to comment
Share on other sites

try running the following code right before you open the stream (right before $fp=fopen($log,"a+")):

 

If this returns "File does not exist" then the path to the file probably has an error, otherwise if it says "File Exists" let me know and i'll look for other methods to troubleshoot

 

A variation to that, try that and see if it works. a+ does not create a file if one does not exist, w+ does. This will allow you to create the log file in the event that one does not exist. If one does exist than it uses the a+ and appends to the file.

 

<?php
if(is_file($log))
   $fileparams = "a+";
else
   $fileparams = "w+";

$fp = fopen($log,$fileparams);
fwrite($fp,"$ip | $file_name | $screen_size | $date | $time");
fclose($fp);
?>

Link to comment
Share on other sites

@ frost: I thought a+ was read AND write while w+ is write only. And both attempt to create.

 

My bad, thats right.  Try adding a b so it reads "ab+" and see if that helps.

 

As for why it is not working, no clue. Try putting the full absolute path instead of using the relative one.

Link to comment
Share on other sites

Now I'm getting these errors:

Warning: move_uploaded_file(/upload/magazine_ad.png): failed to open stream: HTTP wrapper does not support writeable connections. in main.php on line 524

 

Warning: move_uploaded_file(): Unable to move '/tmp/phpML5MS8' to '/upload/magazine_ad.png' in main.php on line 524

 

What does that mean?

Link to comment
Share on other sites

Now I'm getting these errors:

Warning: move_uploaded_file(/upload/magazine_ad.png): failed to open stream: HTTP wrapper does not support writeable connections. in main.php on line 524

 

Warning: move_uploaded_file(): Unable to move '/tmp/phpML5MS8' to '/upload/magazine_ad.png' in main.php on line 524

 

What does that mean?

 

that is because according to http://us2.php.net/manual/en/function.move-uploaded-file.php the function needs to have two parameters:

1. the temporary filename/filepath

2. the destination filename/filepath

Link to comment
Share on other sites

Here's my upload.php file:

<?php
require("incl/main.php");

$self = $_SERVER["PHP_SELF"];
$submit = $_POST["upload_submit"];
$temp_file_name = trim($_FILES["upload"]["tmp_name"]);
$file_name = trim($_FILES["upload"]["name"]);
$upload_dir = "upload/";
$upload_log_dir = "upload/upload_log/";
$max_file_size = 2000000;
$banned_array = array("");
$ext_array = array(".jpg",".gif",".jpeg",".png",".bmp",".tif");

if (($submit) AND ($temp_file_name)) {
print upload_files($temp_file_name, $file_name, $upload_dir, $upload_log_dir, $max_file_size, $banned_array, $ext_array);
}
?>

<form action="<?php print "$self"; ?>" method="post" enctype="multipart/form-data" name="uploader">
  Upload: <input name="upload" type="file" size="50">
  <input type="submit" name="upload_submit" value="Submit">
</form>

 

And here's the function in my main.php file:

<?php
function upload_files($temp_file_name, $file_name, $upload_dir, $upload_log_dir, $max_file_size, $banned_array, $ext_array) {
dbconnect();

//Get Day and Time Variables
$m = date("m");          //Get month
$d = date("d");          //Get day
$y = date("Y");          //Get year
$date = date("m/d/Y");   //Get today's date
$time = date("h:i:s A"); //Get now's time

//Get User Passed Variables
$temp_file_name = trim($temp_file_name);   //Trim Temp File Name
$file_name = trim(strtolower($file_name)); //Trim File Name
$upload_dir = trim($upload_dir);           //Trim Upload Directory
$upload_log_dir = trim($upload_log_dir);   //Trim Upload Log Directory
$max_file_size = trim($max_file_size);     //Trim Max File Size

//Figure if last character for the upload directory is a back slash
$ud_len = strlen($upload_dir);                     //Get upload log directory size
$last_slash = substr($upload_dir, $ud_len - 1, 1); //Get Last Character
if ($last_slash <> "/") {                          //Check to see if the last character is a slash
	$upload_dir = $upload_dir."/";                 //Add a backslash if not present
} else {
	$upload_dir = $upload_dir; //If backslash is present, do nothing
}

//Figure if last character for the upload log directory is a back slash
$udl_len = strlen($upload_log_dir);                     //Get upload log directory size
$last_slash = substr($upload_log_dir, $udl_len - 1, 1); //Get Last Character
if ($last_slash <> "/") {                               //Check to see if the last character is a slash
	$upload_log_dir = $upload_log_dir."/";              //Add a backslash if not present
} else {
	$upload_log_dir = $upload_log_dir; //If backslash is present, do nothing
}

//Validate the extension array
foreach ($ext_array as $key => $value) {            //Start extension loop
	$first_char = substr($value, 0, 1);             //Get first character
		if ($first_char <> ".") {                   //If not a period,
			$extensions[] = ".".strtolower($value); //Write value with a period to a new array
		} else {                                    //Else
			$extensions[] = strtolower($value);     //Write the value to a new array
		}
}

//Get Counts
$ext_count = count($extensions);      //Count the number of extensions
$banned_count = count($banned_array); //Count the number of banned users

//Figure if anyone is banned
if ($banned_count >= 1) {   //If number of banned users if 1 or greater
	$banned_users = "TRUE"; //Set banned_users to TRUE
}

//Get server constants
$ip = $_SERVER['REMOTE_ADDR']; //Get IP address
$self = $_SERVER['PHP_SELF'];  //Get PHP Self
$site = $_SERVER['HTTP_HOST']; //Get Start of Web URL
$ip = $_SERVER['REMOTE_ADDR']; //Get IP Address

//Set file size variables
$kb = 1024;       //Set KB
$mb = 1024 * $kb; //Set MB
$gb = 1024 * $mb; //Set GB
$tb = 1024 * $gb; //Set TB

//Get The File's Extension
$extension = strtolower(strrchr($file_name, ".")); //Get the file extension

//Validate Extension
foreach ($extensions as $key => $value) { //Start extract loop of valid extensions
	if ($value == $extension) {           //If extension is equal to any in the array
		$valid_extension = "TRUE";        //Set valid extension to TRUE
	}
	$all_ext .= $value.", ";              //Get all the extensions
}
$all_ext_len = strlen($all_ext);              //Get the number of characters
$all_ext = substr($all_ext, 0, $all_ext_len - 2); //Extract all text except the last (2) characters

//Validate the file's size
$size = filesize($temp_file_name); //Get the size of the file
	if ($size > $max_file_size) {  //Set over limit statement
		$over_limit = "TRUE";      //Set over_limit to TRUE
	}

//Get the size of the uploaded file in other than bytes
if ($size < $kb) {
	$screen_size = "$size Bytes"; //Set screen_size in bytes, if applicable
} elseif ($size < $mb) {
	$final = round($size/$kb, 2);
	$screen_size = "$final KB"; //Set screen_size in kilo-bytes, if applicable
} elseif ($size < $gb) {
	$final = round($size/$mb, 2);
	$screen_size = "$final MB"; //Set screen_size in mega-bytes, if applicable
} else if($size < $tb) {
	$final = round($size/$gb, 2);
	$screen_size = "$final GB"; //Set screen_size in giga-bytes, if applicable
} else {
	$final = round($size/$tb, 2);
	$screen_size = "$final TB"; //Set screen_size in tera-bytes, if applicable
}

//Get the size of the max file size in other than bytes
if ($max_file_size < $kb) { //Set screen_max in bytes, if applicable
	$screen_max = "$max_file_size Bytes";
} elseif ($max_file_size < $mb) {
	$final = round($max_file_size/$kb, 2);
	$screen_max = "$final KB"; //Set screen_max in kilo-bytes, if applicable
} elseif ($max_file_size < $gb) {
	$final = round($max_file_size/$mb, 2);
	$screen_max = "$final MB"; //Set screen_max in mega-bytes, if applicable
} else if($max_file_size < $tb) {
	$final = round($max_file_size/$gb, 2);
	$screen_max = "$final GB"; //Set screen_max in giga-bytes, if applicable
} else {
	$final = round($max_file_size/$tb, 2);
	$screen_max = "$final TB"; //Set screen_max in tera-bytes, if applicable
}

//Validate the banned users list
if ($banned_users) { //If banned users are present
	foreach($banned_array as $key => $value) { //Start extraction of banned users from the array
		if ($value == $ip) {                   //If the user's IP address is found in list, continue
			$banned_ip = "TRUE";               //and set the banned_ip to TRUE
		}
	}
}

//Start the validation process
if ($banned_ip) {
	$result = "You have been banned from uploading any files to this directory!";
	$log = $upload_log_dir."banned.txt";                  //Log Banned File Name
	$fp = fopen($log, "a+");                               //Set File Pointer
	fwrite($fp, "$ip tried to upload on $date at $time."); //Write File
	fclose($fp);                                          //Close File Pointer
} elseif (!$valid_extension) {
	return FALSE;
} elseif ($over_limit) {
	return FALSE;
} else {
	if (is_uploaded_file($temp_file_name)) {
		if (move_uploaded_file($temp_file_name, $upload_dir . $file_name)) {
			$log = $upload_log_dir.$y."_".$m."_".$d.".txt";                //Log File Name
			if(is_file($log)) {
				$fileparams = "a+";
			} else {
				$fileparams = "w+";
			}
			$fp = fopen($log, $fileparams);
			fwrite($fp, "$ip | $file_name | $screen_size | $date | $time");
			fclose($fp);
			/*$fp = fopen($log,"a+");                                        //Set File Pointer
			fwrite($fp,"$ip | $file_name | $screen_size | $date | $time"); //Write File
			fclose($fp);*/                                                 //Close File Pointer
			return TRUE;
		} else {
			$result =  "Your file could not be uploaded, please try again.";
			return FALSE;
		}
	} else {
		$result =  "Your file could not be uploaded, please try again.";
		return FALSE;
	}
}
}
?>

 

I also read this in the manual:

Description

bool move_uploaded_file ( string $filename, string $destination )

 

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

 

Is using $_FILES bad in light of that? But how else can I get all the information that I need?

Link to comment
Share on other sites

Warning: move_uploaded_file(/upload/magazine_ad.png): failed to open stream: HTTP wrapper does not support writeable connections. in main.php on line 524

 

Warning: move_uploaded_file(): Unable to move '/tmp/phpML5MS8' to '/upload/magazine_ad.png' in main.php on line 524

 

Still this.

Link to comment
Share on other sites

perhaps try removing the beginning forwardslash from the path?  either that or specify your destination path more explicitly (ie. /usr/var/www/ or whatever your full path is).  what is the directory structure you're using?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.