/    Sign up×
Community /Pin to ProfileBookmark

uploading a file

Hi, i m trying to build one function which the end user can upload a non extension file name for example, index.html, the end user need to delete the extension and rename it to index. this is due to integration with another system which can only read non extension file name.

[code=php]
<?
if (isset ($HTTP_POST_FILES[‘txtFile’])){

$file = $HTTP_POST_FILES[‘txtFile’];
if ($file[‘size’] > 0) {
if (!is_uploaded_file($file[‘tmp_name’])) {
echo “<script languague = ‘JavaScript’>”;
echo “alert(‘Not an authentic file!’)”;
echo “</script>”;

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

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

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

$name = $file[‘name’];
$extension = strtolower(substr($name, -4));

$newname = “test”;
$file[‘name’] = $newname;
$file = $_FILES[‘txtFile’][‘name’];

if ($extension == “”){
$uploaddir = ‘upload/’;
$uploadfile = $uploaddir . $newname;

if (move_uploaded_file($HTTP_POST_FILES[‘txtFile’][‘tmp_name’], $uploadfile)) {
echo “correct”;
}
}else{
echo “<script languague = ‘JavaScript’>”;
echo “alert(‘not uploaded! Please upload again!’)”;
echo “</script>”;
}
}
}?>
<form enctype=”multipart/form-data” method=”post” name=”upload”>
<input type=”text” name=”txtDir”>
<table width=”100%” border=”0″ cellspacing=”1″ cellpadding=”2″>
<tr>
<td class=”small” width=”20%”>Upload&nbsp;<input name=”txtFILE” type=”file” onchange = “document.upload.txtDir.value = this.value” ></td>
<td><input type=”submit” name=”Upload” value=”Upload”></td>
</tr>
<td></td>
</tr>
</table>
</form>
[/code]

What’s wrong with the code? and how come it is not working?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@beginnerzauthorMar 06.2006 — [code=php]
$file = $HTTP_POST_FILES['txtFile'];
print_r ("<PRE>");
print_r ($file);
print_r ("</PRE>");
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'upload/';
//echo $uploadsDirectory;
if(isset($_POST['Upload'])){
if(@is_uploaded_file($file['tmp_name'])){
echo "uploadable<BR>";
}else{
echo "unuploadable<BR>";
}
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$file['name']))
{
$now++;
}
echo $file['tmp_name']."<BR>";
echo $uploadFilename."<BR>";
if(@move_uploaded_file($file['tmp_name'], $uploadFilename)){
echo "uploaded<br>";
}else{
echo "not uploaded<BR>";
}

}
<form enctype="multipart/form-data" action="" method="post" name="upload">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td class="small" width="20%">Upload&nbsp;<input name="txtFile" type="file" ></td>
<td><input type="submit" name="Upload" value="Upload"></td>
</tr>
<td></td>
</tr>
</table>
</form>
[/code]
×

Success!

Help @beginnerz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...