home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

File upload script in PHP - File check fails.

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2155
Discussions: 200,936, Posts: 2,379,243, Members: 246,301
Old January 9th, 2006, 03:52 PM   Digg it!   #1 (permalink)
Ultimate Member
 
PyroSama's Avatar
 
Join Date: Nov 2002
Location: Boise, Idaho
Posts: 2,782
Send a message via ICQ to PyroSama Send a message via MSN to PyroSama
File upload script in PHP - File check fails.

I am working on an upload script in PHP but it has some issues that I cant seem to resolve....

Every file I attempt to upload fails the file extention check and returns an error message. If some one would take a few moments to find the obvious mistake that I have made some place it would be much apricciated.


http://srcbin.com/viewcode.php?id=95


Implemented at http://www.srcbin.com/upload.php


Thanks,
PyroSama
__________________
JOIN [FaD] | TEAM NUMBER 2037 | http://www.techimo.com/forum/t121132.html
PyroSama is offline   Reply With Quote
Old January 9th, 2006, 04:04 PM     #2 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
put some echo statements in to see what its actually getting from
Code:
$file_ext = strtolower(substr($file_name, "."));
, cause I'm not sure if that substr command is right.

seems like all you need is
Code:
strtolower(substr($file_name, strlen($file_name)-4));

Last edited by VHockey86 : January 9th, 2006 at 04:09 PM.
VHockey86 is offline   Reply With Quote
Old January 9th, 2006, 04:40 PM     #3 (permalink)
Ultimate Member
 
PyroSama's Avatar
 
Join Date: Nov 2002
Location: Boise, Idaho
Posts: 2,782
Send a message via ICQ to PyroSama Send a message via MSN to PyroSama
I echo'd $file_ext and it returned the full name of the file I am attempting to upload when using your snippet it always returns the error that the file already exists (checks md5 vs database)

PyroSama
PyroSama is offline   Reply With Quote
Old January 9th, 2006, 05:03 PM     #4 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
so let me get this straight... you are using the md5 output of the file itself, or the md5 of the filename as the name in the database?

Last edited by VHockey86 : January 9th, 2006 at 05:08 PM.
VHockey86 is offline   Reply With Quote
Old January 9th, 2006, 05:55 PM     #5 (permalink)
Ultimate Member
 
Join Date: Dec 2004
Posts: 1,558
Try using this modified code... it should fix your extension issue, and your 'timeout' issue
PHP Code:
<?
// Database Configuration
require("config.php");

//Set .INI params to support uploading
ini_set('post_max_size'5242880);    // Set upload limit (5MB)
ini_set('upload_max_filesize'5242880);    // Enforce upload limit
ini_set('max_execution_time'2400);    // Increase 'timeout' time

//Set Variables
$uploadpath 'uploads/files/';    // Directory For File Uploads
$upload $_POST['file'];        // Code Field
$recipiant_email $_POST['tbEmail22'];    // Email of Recipiant
$recipiant $_POST['tbEmail2'];    // Recipiant Name
$comments $_POST['textarea2'];    // File Comments
$type $_POST['select'];    // File Type
$file_hash md5($upload);    // Hash for File Name
$file_upload_hash $uploadpath $file_hash;    // Hash Path/File
$file $_POST['file'];    // File Name
$file_name $uploadpath $file;    // Path/Name
$legal_extentions = array("zip","rar","gz","7z");    // Legal File Extentions
$file_size '5242880';    // Maximum File Size - 5 MB

// Start PHP session cookie for users later on.
session_start();

// If connection fails...
if (!$conn){
    exit(
"Connection Failed: The server is down. Sorry for the dellay, please try again.");    // Print error message.
}

// Not sure but does this do what I did above?
@mysql_select_db("srcbin_srcbin") or die("Unexpected database error!");

if(
file_exists($file_upload_hash)){
    
$get_id mysql_query("SELECT * FROM file where file = '" $file_upload_hash "'");    // Select row containing md5 of submited code
    
if( $get_id && $file_upload_hash mysql_fetch_object$get_id ) )    // Get row based on $get_id
    
$id $file_upload_hash -> id;    // Select entry for ID
        
die("Target already exists. - As soon as file renaming works properly this will redirect.");    // Print message and list path from $path echoing $viewpath and the entry for ID ($id)
}

//$file_type = $_FILES['userfile']['type'];    // Check File Type By MIME
$file_name $_FILES['userfile']['name'];    // Get File Name
$file_ext strtolower(end(explode(".",$file_name)));    // File Extension

// File Size Check - Need to set size right now it's some ungodly ammount.
if (!$_FILES['userfile']['size'] > $file_size)
{
    die(
"The file you are attempting to upload is too large.");
}

// File Extention Check
if (!in_array ($file_ext$legal_extentions))
{
    die(
"The file you are attempting to upload is not supported by this server.");
}

// Add File to Database
$upload_file "INSERT INTO file(recipeint, recipiant_email, file, name, type) VALUES ('" $recipiant "', '" $recipiant_email "', '" $file_hash "', '" $file "', '" $type "')";
mysql_query($upload_file);

$fp fopen($file_upload_hashw);    // Open file with wright permissions.
fwrite($fp$upload);    // Write code to file.
fclose($fp);    // Close file.

$id $_GET['id'];    // File ID
?>
If the above code doesn't fix your 'timeout' issue, remove this code:
PHP Code:
//Set .INI params to support uploading
ini_set('post_max_size'5242880);    // Set upload limit (5MB)
ini_set('upload_max_filesize'5242880);    // Enforce upload limit
ini_set('max_execution_time'2400);    // Increase 'timeout' time 
And make a file named .htaccess put the following code into it, and upload it to the location your upload script is running from:
Code:
php_value post_max_size 5242880
php_value upload_max_filesize 5242880
php_value max_execution_time 2400
Also, in your database, make sure you've got "recipeint" spelled the same way:
Quote:
$upload_file = "INSERT INTO file(recipeint, recipiant_email, file, name, type) VALUES ('" . $recipiant . "', '" . $recipiant_email . "', '" . $file_hash . "', '" . $file . "', '" . $type . "')";

Quote:
// Not sure but does this do what I did above?
@mysql_select_db("srcbin_srcbin") or die("Unexpected database error!");

Ummm, not really sure... I'd have to see your db config file, and what $conn is.

And I'm not all to sure about the MD5/database issue...

Eh hope that helps ya though.
__________________
"Be quiet, Brain, or I'll stab you with a Q-tip"
-Homer Simpson
large_nostril is offline   Reply With Quote
Old January 10th, 2006, 07:17 AM     #6 (permalink)
Ultimate Member
 
PyroSama's Avatar
 
Join Date: Nov 2002
Location: Boise, Idaho
Posts: 2,782
Send a message via ICQ to PyroSama Send a message via MSN to PyroSama
Hey thank you so much!

That hit the spot

I have a few other things to work out with the script but the one major issue has been resolved

Thanks again,
PyroSama
PyroSama is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
download/upload zip file Naz Webmastering and Programming 5 July 17th, 2004 03:31 PM
Script file creator with variables muno General Tech Discussion 4 March 16th, 2004 08:20 AM
PHP script to search file? e980238 Webmastering and Programming 3 August 10th, 2003 10:31 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2903)
windows 7 problem (6)
CPU fan stops spinning randomly (8)
Wireless Televisions. (8)
California Passes Anti-Flat-HDTV Le.. (41)
Obama the Muslim (14)
Is the PSU I received dead? (11)
windows vista security holes (9)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
Recent Discussions
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Is the PSU I received dead? (11)
Have you switched yet? (85)
Regular Build (4)
windows 7 problem (6)
Point and Shoot Camera Suggestions. (2)
Modern Warfare 2 freeze (13)
Wireless Televisions. (8)
wireless user (1)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)


All times are GMT -4. The time now is 12:17 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28