I am working on a file upload class for my website. I keep getting the following error when I try to run it I keep looking at the script and I can not find any problems with it.
Warning: filesize() [function.filesize]: Stat failed for dialup2.jpg (errno=2 - No such file or directory) in /home/rfwrangl/domains/rfwrangler.frih.net/public_html/Resources/PHPModules/Classes/file_upload_class.php on line 105
I looked at the php settings on the server and found that there is not a temp upload file set on the server. Is there a way to set this and what do I need to do??
I thought that maybe people would have a better idea if I included the code that I am using.
Here is the error that I am getting:
| Quote: |
| Warning: filesize() [function.filesize]: Stat failed for 5041l.jpg (errno=2 - No such file or directory) in /home/rfwrangl/domains/rfwrangler.frih.net/public_html/Resources/PHPModules/Classes/file_upload_class.php on line 90 |
I am not sure what I have done wrong
Here is the code from my site:
| Code: |
<?php
//This starts the class and sets up the variables needed for the class.
class upload_files {
var $temp_file_name;
var $file_name;
var $upload_dir;
var $upload_log_dir;
var $max_file_size;
var $banned_array;
var $ext_array;
function validate_extension() {
$file_name = trim($this->file_name);
$extension = strtolower(strrchr($file_name,"."));
$ext_array = $this->ext_array;
$ext_count = count($ext_array);
if(!$file_name) {
return false;
} else {
if(!$ext_array) {
return true;
} else {
foreach($ext_array as $value) {
$first_char = substr($value,0,1);
if ($first_char <> ".") {
$extensions[] = ".".strtolower($value);
} else {
$extensions[] = strtolower($value);
}
}
foreach($extensions as $value) {
if($value == $extension) {
$valid_extension = "TRUE";
}
}
if($valid_extension) {
return true;
} else {
return false;
}
}
}
}
function validate_size() {
$temp_file_name = trim($this->temp_file_name);
$max_file_size = trim($this->max_file_size);
if (!$temp_file_name) {
$size = filesize($temp_file_name);
if($size > $max_file_size) {
return false;
} else {
return true;
}
} else {
return false;
}
}
function existing_file() {
$file_name = trim($this->file_name);
$upload_dir = $this->get_upload_directory();
if($upload_dir == "ERROR") {
return true;
} else {
$file = $upload_dir.$file_name;
if(file_exists($file)) {
return true;
} else {
return false;
}
}
}
function get_file_size() {
$temp_file_name = trim($this->temp_file_name);
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if($temp_file_name) {
$size = filesize($temp_file_name);
if($size < $kb) {
$file_size = "$size Bytes";
}
elseif($size < $mb) {
$final = round($size/$kb,2);
$file_size = "$final KB";
}
elseif($size < $gb) {
$final = round($size/$mb,2);
$file_size = "$final MB";
}
elseif($size < $tb) {
$final = round($size/$gb,2);
$file_size = "$final GB";
} else {
$final = round($size/$tb,2);
$file_size = "$final TB";
}
} else {
$file_size = "ERROR: NO FILE PASSED TO get_file_size()";
}
return $file_size;
}
function get_max_size() {
$max_file_size = trim($this->max_file_size);
$kb = 1024;
$mb = 1024*$kb;
$gb = 1024*$mb;
$tb = 1024*$gb;
if($max_file_size) {
if($max_file_size < $kb) {
$max_file_size = "max_file_size Bytes";
}
elseif($max_file_size < $mb) {
$final = round($max_file_size/$kb,2);
$max_file_size = "$final KB";
}
elseif($max_file_size < $gb) {
$final = round($max_file_size/$mb,2);
$max_file_size = "$final MB";
}
elseif($max_file_size < $tb) {
$final = round($max_file_size/$gb,2);
$max_file_size = "$final GB";
} else {
$final = round($max_file_size/$tb,2);
$max_file_size = "$final TB";
}
} else {
$max_file_size = "ERROR: NO SIZE PARAMETER PASSED TO get_max_size()";
}
return $max_file_size;
}
function validate_user() {
$banned_array = $this->banned_array;
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$count = count($banned_array);
if($count < 1) {
return true;
} else {
foreach($banned_array as $key => $value) {
if($value == $ip . "-" . $cpu) {
return false;
} else {
return true;
}
}
}
}
function get_upload_directory() {
$upload_dir = trim($this->upload_dir);
if($upload_dir) {
$ud_len = strlen($upload_dir);
$last_slash = substr($upload_dir,$ud_len-1,1);
if($last_slash <> "/") {
$upload_dir = $upload_dir."/";
} else {
$upload_dir = $upload_dir;
}
$handle = @opendir($upload_dir);
if($handle) {
$upload_dir = $upload_dir;
closedir($handle);
} else {
$upload_dir = "ERROR";
}
} else {
$upload_dir = "ERROR";
}
return $upload_dir;
}
function get_upload_log_directory() {
$upload_log_dir = trim($this->upload_log_dir);
if($upload_log_dir) {
$ud_len = strlen($upload_log_dir);
$last_slash = substr($upload_log_dir,$ud_len-1,1);
if($last_slash <> "/") {
$upload_log_dir = $upload_log_dir."/";
} else {
$upload_log_dir = $upload_log_dir;
}
$handle = @opendir($upload_log_dir);
if($handle) {
$upload_log_dir = $upload_log_dir;
closedir($handle);
} else {
$upload_log_dir = "ERROR";
}
} else {
$upload_log_dir = "ERROR";
}
return $upload_log_dir;
}
function upload_file_no_validation() {
$temp_file_name = trim($this->temp_file_name);
$file_name = trim(strtolower($this->file_name));
$upload_dir = $this->get_upload_directory();
$upload_log_dir = $this->get_upload_log_directory();
$file_size = $this->get_file_size();
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$m = date("m");
$d = date("d");
$y = date("Y");
$date = date("m/d/Y");
$time = date("h:i:s A",-7200);
if(($upload_dir == "ERROR") || ($upload_log_dir == "ERROR")) {
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";
$fp = fopen($log,"a+");
fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time");
fclose($fp);
return true;
} else {
return false;
}
} else {
return false;
}
}
}
function upload_file_with_verification() {
$temp_file_name = trim($this->temp_file_name);
$file_name = trim(strtolower($this->file_name));
$upload_dir = $this->get_upload_directory();
$upload_log_dir = $this->get_upload_log_directory();
$file_size = $this->get_file_size();
$ip = trim($_SERVER['REMOTE_ADDR']);
$cpu = gethostbyaddr($ip);
$m = date("m");
$d = date("d");
$y = date("Y");
$date = date("m/d/Y");
$time = date("h:i:s A",-7200);
$existing_file = $this->exhisting_file();
$valid_user = $this->validate_user();
$valid_size = $this->validate_size();
$valid_ext = $this->validate_extension();
if(($upload_dir == "ERROR") || ($upload_log_dir == "ERROR")) {
return false;
}
elseif ((((!$valid_user) || (!$valid_size) || (!$valid_ext) || ($existing_file)))) {
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";
$fp = fopen($log,"a+");
fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time");
fclose($fp);
return true;
} else {
return false;
}
} else {
return false;
}
}
}
}
?>
|
and here is the code for the include like I am trying to run it know:
| Code: |
<?php
require 'Resources/PHPModules/Classes/file_upload_class.php';
$upload_class = new Upload_Files;
$upload_class->temp_file_name = trim($_FILES['picture_upload']['name']);
$upload_class->file_name = trim(strtolower($_FILES['picture_upload']['name']));
$upload_class->upload_dir = "UserFiles/Resources/profile_graphics/";
$upload_class->upload_log_dir = "Logs/user_uploads/";
$upload_class->max_file_size = 102400;
$upload_class->banned_array = array("");
$upload_class->ext_array = array(".jpg", ".gif", ".jpeg", ".png");
$valid_ext = $upload_class->validate_extension();
$valid_size = $upload_class->validate_size();
$valid_user = $upload_class->validate_user();
$max_file_size = $upload_class->get_max_size();
$file_size = $upload_class->get_file_size();
$file_exists = $upload_class->existing_file();
if(!valid_ext) {
$result = "The file extension is invalid,";
}
elseif(!$valid_size) {
$result = "The file is too large.";
}
elseif(!$valid_user) {
$result = "You are not allowed to upload to the server.";
}
elseif($file_exists) {
$result = "The fiole that you are uploading already exists";
} else {
$upload_file = $upload_class->upload_file_with_validation();
if(!$upload_file) {
$result = "Your file could not be uploaded.";
} else {
$result = "Your file has been uploaded successfully";
}
}
?> |
I looked at my php settings an found that the path to the temp_file_upload dir is not set I am not sure if it makes a difference.
I've checked the code and ran it myself. There was no problems at all. Are you using Frihost to run this script? As far as I know, Frihost has restricted automated file uploads (using PHP). That's why the file was found not to exist.
Yes I am trying to run this on frihost. Thank you for looking at this. I have been working on a different script for upload files that is working on the site.
I wanted to make sure that I wasn't doing something wrong and that I wasn't missing something obvious. I appreciate your help with this and I will keep that class so that I can maybe use it else where if I need it.