PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 3rd, 2004, 03:29 PM
sita12691 sita12691 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 sita12691 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
issue with file upload in php

hello,

i'm brand new to the world of php, i've spent the last week trying to learn it and making this web page.
I had the form page set to 20 different userfiles with different names (userfile1, userfile2, etc). When
I changed it to a loop, and changed the upload page to the one below, it stopped working correctly.
It jumps right to the 'No File Uploaded' part of the first if statement, which I assume means it thinks the
file size is 0. I have no idea how to fix it, and all help would be greatly appreciated.


<?php
for($c=1; $c<=20; $c++){
$file = $HTTP_POST_FILES['userfile[$c]'];
if ($file['size'] > 0) {

if (! is_uploaded_file($file['tmp_name'])) {
die('Not an authentic uploaded file.');
}

// the number of bytes to read from the file
$bytelen = $file['size'];

// file pointer to the temporary location of th
// uploaded file
$fp = fopen($file['tmp_name'], 'r');

// read in the file data and close the pointer
$data = fread($fp, $bytelen);
fclose($fp);

// massage the data for database insertion
$data = addslashes($data);
$mime = addslashes($file['type']);
$name = addslashes($file['name']);

$file = $_FILES['userfile[$c]']['name'];
$true = checkext($file,"jpg");
echo("It returned '$true'");

if ($true=='1'){

$uploaddir = 'c:/program files/apache group/apache2/htdocs/uploadedpics/';
$uploadfile = $uploaddir . $HTTP_POST_FILES['userfile[$c]']['name'];

print "<pre>";
if (move_uploaded_file($HTTP_POST_FILES['userfile[$c]']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($HTTP_POST_FILES);
echo $uploaddir;
echo $uploadfile;
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($HTTP_POST_FILES);
}
print "</pre>";
}
} else {

// no uploaded file, size was <= 0.
die('No file was uploaded.');
}

//Insert file info into table
$db = mysql_connect("localhost", "root");
mysql_select_db("evm",$db);
$sql = "INSERT INTO filetable (file_name, file_mime, file_data) VALUES ('$name', '$mime', '$data')";
$result = mysql_query($sql);}

function checkext($file,$search)
{
// Looks for $search at the end of the file
$lastpos = strlen($file) - 1;
$diffrence = strlen($search)-1;
$firstpos = $lastpos - $diffrence;
$z = 0;
$x = $firstpos;
while($z <= $diffrence)
{
if($file[$x] == $search[$z])
{
// Yes
$return = 1;
}
else
{
// No!
$return = 0;
}
$x++;
$z++;
}
return $return;
}

?>

Reply With Quote
  #2  
Old May 6th, 2004, 01:59 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
I'll give you a couple quick pointers:

Instead of $HTTP_POST_FILES, try using the superglobal $_FILES.

Secondly, instead of a for loop, try using a foreach loop.
For example:
PHP Code:
foreach ( $_FILES as $file => $file_info ) {
  print 
$file_info['name'] . '<br />'// this is just to test we are getting files



I hope this helps
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > issue with file upload in php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT