Reputation: 2489
I can't seem to get files to be uploaded with PHP. From what I can tell, everything checks out. I even went as far as digging up the old textbook and copied the example straight out of the text and still no go.
At first, I was thinking it was an issue with the directory permissions where the files are being saved so I changed permissions to 777 and still nothing. If anyone has any suggestions I'm all ears :/
Here is the code (HTML):
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<br/>
<input type="submit" value="Upload">
</form>
The PHP file:
<?php
$tmp_name = $_FILES['file1']['tmp_name'];
$path = getcwd() . DIRECTORY_SEPARATOR . 'images';
$name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name'];
$success = move_uploaded_file($tmp_name, $name);
if($success) {
$upload_message = $name . ' has been uploaded.';
echo "$upload_message";
} else {
echo "something went wrong :(";
}
?>
Upvotes: 1
Views: 438
Reputation: 345
I am not quite sure this works or not, have not try it out yet.
<?
$uploadpath = '../images/';
if ($_FILES["file1"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
echo "Type: " . $_FILES["file1"]["type"] . "<br />";
echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file1"]["tmp_name"] . "<br />";
}
if (file_exists($uploadpath . $_FILES["file1"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploadpath . $_FILES["file"]["name"]);
echo "UPLOAD SUCCESS!";
}
?>
Source from : W3school
Upvotes: 0
Reputation: 46602
You should be checking for an upload error before attempting to move it then you can track down the problem: http://www.php.net/manual/en/features.file-upload.errors.php
Heres a basic example:
<?php
error_reporting(-1);
$uploaddir = './images/';
// Check for upload attempt
if(isset($_FILES['file1'])){
$uploadfile = $uploaddir.basename($_FILES['file1']['name']);
// If no error
if($_FILES['file1']['error'] == 0){
//Attempt to move
if (move_uploaded_file($_FILES['file1']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.";
}else{
echo 'Error moving file.';
}
} else {
// Has error
$errors = array(0=>'OK',
1=>'UPLOAD_ERR_INI_SIZE',
2=>'UPLOAD_ERR_FORM_SIZE',
3=>'UPLOAD_ERR_PARTIAL',
6=>'UPLOAD_ERR_NO_TMP_DIR',
7=>'UPLOAD_ERR_CANT_WRITE',
8=>'UPLOAD_ERR_EXTENSION'
);
echo "Error: ".$errors[$_FILES['file1']['error']];
}
}
BTW. Be very wary of allowing uploads.
Upvotes: 2
Reputation: 4287
You need to make sure you have permission to write to upload_tmp_dir, check post_max_size and upload_max_filesize limits in php.ini to be larger than the file you are trying to upload. Also $_FILES contains error information, dump that too to have more information about what isn't happening as it's expected to.
Upvotes: 1