Jump to content

[SOLVED] Uploading Script problems


antwonw

Recommended Posts

Hey, I am trying to write this upload script for my website. But so far there are two things I don't like.

 

1. They are two separate files 'form.html' and 'uploader.php'

2. The stupid thing isn't uploading in any case

 

What I would like to do...

 

1. Get it to work

2. Put it in one file

 

Here is the code for the 'form.html'

 

Upload:
<form action="uploader.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="30">
<input type="submit" value="Upload File">
</form>

 

And here is the code for my 'uploader.php'

 

<?php
if($file_name !="")
{
copy ("$file", "../homes/$file_name")
         or die("Could not copy file");
}
else { die("No file specified"); }
?>

 

So can anyone tell me...

 

1. What is wrong and why it won't upload

2. How I can merge them together

 

Thanks,

Antwonw

Link to comment
https://forums.phpfreaks.com/topic/83682-solved-uploading-script-problems/
Share on other sites

try this

 

<?php
echo'
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>';

$uploaddir = '/tp-images/Image/'; // file directory...
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "file was not upload ".$_FILES['userfile']['name']."";
}

?>

I would recommend using the example in the manual:

 

http://www.php.net/features.file-upload

 

Specifically, example two.  That should get your file uploaded.

 

To use a single file, do some thing like this:

 

<?php
if (count($_FILES) > 0) {
  /* code to handle file upload (see php manual) */
  
  //pseudo code:
  if ($file_uploaded_successfully == true) {
    $message = "Your file has been uploaded";
  } else {
    $message = "There was an error uploading the file";
  }
}

?>

<!-- Put your html upload form here -->

<?php echo $message; ?>

 

Make sure that the form submits to the same page it's on.  The premise is to check to see if the form was submitted ( "if (count($_FILES) > 0) ...' ), if it has, process the upload and set a message to display for the user.

I finally got it to work with this...

 

Upload File:
<form name=upload action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="30">
<input type="submit" value="Upload">
</form>

<?php

$DestinationDir = "/xampp/htdocs/upload/";
$DestinationFile = $DestinationDir . $_FILES['file']['name'];
If (move_uploaded_file($_FILES['file']['tmp_name'], $DestinationFile)) {
Echo "File uploaded successfully.";
}Else{
Echo $_FILES['file']['error'];
}
?>

 

Only problem is I am wanting to add it to another page and I used this...

 

<td>
<?php
include( "upload.php" );
?>
</td>

 

Well in the other page it doesn't upload. But if I got directly to my 'upload.php' file it does upload.

 

What am I doing wrong?

 

Thanks,

Antwonw

// add this to the other page...

 

Upload File:

<form name=upload action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="file" size="30">

<input type="submit" value="Upload">

</form>

 

call this one upload.php

 

<?php

 

$DestinationDir = "/xampp/htdocs/upload/";

$DestinationFile = $DestinationDir . $_FILES['file']['name'];

If (move_uploaded_file($_FILES['file']['tmp_name'], $DestinationFile)) {

Echo "File uploaded successfully."; // redirect user here to the main page

}Else{

Echo $_FILES['file']['error'];

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.