Current location: Hot Scripts Forums » Programming Languages » PHP » PHP File upload not working.... WHYYY!


PHP File upload not working.... WHYYY!

Reply
  #1 (permalink)  
Old 03-22-10, 03:01 PM
timbaker1991's Avatar
timbaker1991 timbaker1991 is offline
Newbie Coder
 
Join Date: Dec 2009
Location: London, UK
Posts: 38
Thanks: 2
Thanked 3 Times in 3 Posts
PHP File upload not working.... WHYYY!

Hi I've got this file upload for a site, it puts the files in to a directory on the site called /f/sf (thats the path from the route folder) well it should do that at least but for some reason it doesn't upload them and I cant figure it out!

Also I know there's a line which connects to a database but that works correctly I know because the details of the file are being posted in to the database, so i figured the PHP section is the best place to come!

Here's the code
PHP Code:

<?php

//Checks if there is a login cookie
if(isset($_COOKIE['******']));
else{
header'Location: http://www.****.php?error=Sorry+please+login' );
}

//This is the directory where images will be saved
$folder "f/sf/";

//This gets all the other information from the form
$name=$_COOKIE['*****'];
$pic=($_FILES['photo']['name']);
$ip getenv('REMOTE_ADDR');



//This function separates the extension from the rest of the file name and returns it 
function findexts ($filename

$filename strtolower($filename) ; 
$exts split("[/\\.]"$filename) ; 
$n count($exts)-1
$exts $exts[$n]; 
return 
$exts


//This applies the function to our file 
$ext findexts ($_FILES['photo']['name']) ; 

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
$ran rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 $ran.".";

//This combines the directory, the random file name, and the extension
$target $ran2.$ext

// Connects to your Database
mysql_connect("***") or die(mysql_error()) ;
mysql_select_db("****") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `file_sf` (name, ip, photo, target) VALUES ('$name', '$ip', '$pic', '$target')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $folder))
{

//Tells you if its all ok
header'Location: http://www.****.php?read=File+uploaded+successfully' );
}
else {

//Gives and error if its not
header'Location: http://www.******.php?read=Sorry,+there+was+a+problem+uploading+the+file.+Please+try+again');
}
?>

Just to clarify that code should do all the following
- rename the file with a random number to prevent files with the same name being over written
- then post this random name (with the file extension on the end) to the database < this works currently
and upload the file to the directory

Thank in advanced, Tim
Reply With Quote
  #2 (permalink)  
Old 03-22-10, 04:01 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Instead of rand, you could use: PHP: tempnam - Manual

Try using
PHP Code:

$folder.=$target
, because I think $destination of the function call must be a filename, not a directory.

PHP: move_uploaded_file - Manual
Reply With Quote
  #3 (permalink)  
Old 03-24-10, 04:09 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
If that doesn't work, make sure you are pointing to the directory correctly. A test file in the upload directory with:
PHP Code:

<?php
echo 'Script Path: ' $_SERVER['SCRIPT_NAME'] . '<br/>Absolute Path: ' $_SERVER['SCRIPT_FILENAME'] . '<br/>';
?>
should tell you.

If that doesn't work, check to make sure your server allows uploads via php. (should tell you in php_info())
Reply With Quote
  #4 (permalink)  
Old 10-09-10, 03:21 PM
David Edward David Edward is offline
New Member
 
Join Date: Oct 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I here have same problem, anyone help?
Reply With Quote
  #5 (permalink)  
Old 10-12-10, 08:06 AM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
Quote:
Originally Posted by David Edward View Post
I here have same problem, anyone help?
We would need to see some code to see where you went wrong. Make sure you follow the upload examples given at PHP: move_uploaded_file - Manual
__________________
Yep, it's a signature...
Reply With Quote
Reply

Bookmarks

Tags
mysql, php file upload


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP upload script alej469 Job Offers & Assistance 5 05-18-06 05:01 AM
Upload a file to a fix web folder of a website without using FTP software sujata_ghosh Perl 4 05-10-06 09:10 AM
File Upload as Easy as Drag And Drop. e4c5 General Advertisements 0 11-09-05 09:00 AM
PHP file upload status screen qwkhyena Script Requests 1 02-09-05 10:57 AM
file upload csmaster2005 PHP 1 06-16-04 06:57 PM


All times are GMT -5. The time now is 08:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.