Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 3rd, 2005, 6:16 AM   #1
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
Uploading, Making Directory.

Ok, so basically I want it so that whenever someone uploads a file to my server, it makes a directory with a given name, and puts the file in the directory. If that wasn't clear, I'm sure you'll be able to figure what I mean after looking at my script. Anyway, the new directory part is working, but my file won't seem to upload. Script:
HTML PAGE:
<form action="/uploadScript.php" enctype="multipart/form-data" method="post">
<input type="text" name="dirName" />
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />
<input type="file" name="fileUpload" />
<br />
<input type="submit" value="Create Directory" />
</form>

PHP PAGE:
<?
$dirName = $HTTP_POST_VARS['dirName'];
$slashName = "\apache2triad\htdocs\\" . $dirName;
mkdir($slashName, 0777);
print "Name: <font color='red'>".$HTTP_POST_FILES['fileUpload']['name']."</font><br>\n";
print "Size: <font color='red'>".$HTTP_POST_FILES['fileUpload']['size']."</font><br>\n";
if (is_uploaded_file($HTTP_POST_FILES['fileUpload']['tmp_name'])) {
move_uploaded_file($HTTP_POST_FILES['fileUpload']['tmp_name'], $slashName);
print "<font color='red'><blink>Success!</blink></font>";
} else {
print "<font color='red'><blink>Error!</blink></font>";
}
?>

I think the problem lays in the move_ uploaded file() function. Any help is apperciated!
TCStyle is offline   Reply With Quote
Old Jun 3rd, 2005, 7:12 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The problem is most likely to do with the slashName. If you're using Windows, it should be "C:\\Apache2Triad\htdocs\\"; if not, then the slashes are the wrong way round. And please, use [code] and [php] tags.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 4th, 2005, 9:21 AM   #3
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
.

Quote:
Originally Posted by Ooble
The problem is most likely to do with the slashName. If you're using Windows, it should be "C:\\Apache2Triad\htdocs\\"; if not, then the slashes are the wrong way round. And please, use [code] and [php] tags.
Sorry for not using the [code] tag, I'm still sort of new to these forums. The $slashName variable doesn't seem to be the problem. I've tried both your suggestions, nothing seems to work. The directory is created fine, but no file is uploaded?
TCStyle is offline   Reply With Quote
Old Jun 4th, 2005, 8:04 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Can I ask where 'tmp_name' came from? I'm no expert on file-handling on PHP, so forgive me if I'm wrong, but I don't see it anywhere else in your code.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 4th, 2005, 10:31 PM   #5
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
tmp

$HTTP_POST_FILES['fileUpload']['tmp_name'] is a randomized name the your server temporarily gives the file that was uploaded. I'm not sure why it does that, but until you specify a place for the file to go, you need to refer to it by its temporary name.
TCStyle is offline   Reply With Quote
Old Jun 5th, 2005, 6:20 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I've been reading up on file uploads, and I've noticed that PHP provides an error code in the event of an error: $_FILES['fileUpload']['error']. Try printing that and finding out if it tells you what's happening. The error code list is at http://www.php.net/manual/en/feature...oad.errors.php.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 8th, 2005, 12:07 PM   #7
Aryo Sanjaya
Newbie
 
Aryo Sanjaya's Avatar
 
Join Date: Jun 2005
Location: Malang - Indonesia
Posts: 2
Rep Power: 0 Aryo Sanjaya is on a distinguished road
Send a message via Yahoo to Aryo Sanjaya
Target File and Dirname

Hello TCStyle,

I have found two important things on your code, that i think it is not correct.

First: as far as i know, we cannot use <input type='file'> and <input type='text'> as so easy... i forgot where is the description about it, but when i remark the line $dirName = $HTTP_POST..., then the filesize is in correct size.

Second: when we move uploaded file, we have to define the target filename, not just the target directory.

Here is work for me: (your code is remarked)

<?php

//  $dirName = $HTTP_POST_VARS['dirName'];
$dirName = "test";
if ($dirName)
{
// $slashName = "\apache2triad\htdocs\\" . $dirName;
  $slashName = "c:\apache\htdocs\\$dirName\\".$_FILES['fileUpload']['name'];
// mkdir($slashName, 0777);
  print "Name: <font color='red'>".$_FILES['fileUpload']['name']."</font><br>\n";
  print "Size: <font color='red'>".$_FILES['fileUpload']['size']."</font><br>\n";
  if (is_uploaded_file($_FILES['fileUpload']['tmp_name'])) {
      move_uploaded_file($_FILES['fileUpload']['tmp_name'], $slashName);
  print "<font color='red'><blink>Success!</blink></font>";
  } else {
  print "<font color='red'><blink>Error!</blink></font>";
  }
}
?>
__________________
Aryo Sanjaya

Sangkan paraning dumadi
Manunggaling kawulo gusti
Aryo Sanjaya is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC