soycharliente Posted July 16, 2007 Share Posted July 16, 2007 Where can I post questions about using the quick code scripts? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 16, 2007 Share Posted July 16, 2007 What quick code scripts? Are they PHP? If they are Third-Party scripts, use that forum. If they are just basic code script or snippets feel free to post here. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 16, 2007 Author Share Posted July 16, 2007 I took it from the main site quick code section. I just don't know where to post. I tried to post my question underneath it but it says I "don't have access to that page on the server." Â Yes php. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 16, 2007 Share Posted July 16, 2007 Just post questions here, nothing wrong with that. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 16, 2007 Author Share Posted July 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 16, 2007 Share Posted July 16, 2007 Are you sure that from where main.php is located there is a folder called upload and upload_log inside that folder and that the upload_log folder is chmoded to 777 ? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 16, 2007 Author Share Posted July 16, 2007 Yes they are. I set them myself. Quote Link to comment Share on other sites More sharing options...
saf Posted July 16, 2007 Share Posted July 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
per1os Posted July 16, 2007 Share Posted July 16, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 16, 2007 Author Share Posted July 16, 2007 @ frost: I thought a+ was read AND write while w+ is write only. And both attempt to create. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 16, 2007 Share Posted July 16, 2007 @ 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. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 16, 2007 Share Posted July 16, 2007 i've found some issues trying to operate on files using absolute paths on a Windows machine (running the WAMP usuals) - try using a symlink and seeing if that helps, if you're running on windows. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 17, 2007 Author Share Posted July 17, 2007 My hosting is Linux. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 17, 2007 Author Share Posted July 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
saf Posted July 17, 2007 Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 17, 2007 Share Posted July 17, 2007 i'd guess it's because you're trying to write via its URL wrapper, as opposed to its local path. if not, i couldn't tell you without seeing what paths and variables exactly you're using. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 23, 2007 Author Share Posted July 23, 2007 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? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 23, 2007 Author Share Posted July 23, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 24, 2007 Author Share Posted July 24, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
per1os Posted July 24, 2007 Share Posted July 24, 2007 Using $_FILES is fine, what is the problem you are having, you never stated and/or restated the problem. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 24, 2007 Author Share Posted July 24, 2007 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. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2007 Share Posted July 24, 2007 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? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 24, 2007 Author Share Posted July 24, 2007 Remove the beginning forward slash? There isn't one. Here's my upload.php file: <?php //... $upload_dir = "upload/"; $upload_log_dir = "upload/upload_log/"; //... ?> Â What does this mean? HTTP wrapper does not support writeable connections. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2007 Share Posted July 24, 2007 it means it thinks you're passing it a URL as a write path, as opposed to an absolute URL. i have no idea why it's assuming you're passing it a URL, unless there's a hidden setting somewhere in the .ini? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 24, 2007 Author Share Posted July 24, 2007 What setting would I be looking for? I don't think I even have access to my .ini file. Is it something I could see using php_info()? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.