Reputation: 17
I am trying to upload a file and save it in a folder. It prints: successful but the file is not in the destination folder. Do I give any sort of permissions? How do I give them? Also how do I specify destination folder path, i.e do I use \ or / ?
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
echo "1";
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
echo "2";
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
echo "Sarang";
move_uploaded_file($file_tmp,"/uploads".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>
<ul>
<li>Sent file: <?php echo $_FILES['image']['name']; ?>
<li>File size: <?php echo $_FILES['image']['size']; ?>
<li>File type: <?php echo $_FILES['image']['type'] ?>
</ul>
</form>
</body>
</html>
Upvotes: 0
Views: 60
Reputation: 434
This line
move_uploaded_file($file_tmp,"/uploads".$file_name);
should be something like this:
$target_file = __DIR__ . "/uploads/" . $file_name; // don't forget the slash
move_uploaded_file($file_tmp, $target_file);
Destination path must be the full path on the server or relative to the current working directory.
A path like /uploads/
will be interpreted as starting from root /
directory if you use an UNIX OS, and if you don't have this path it won't save it.
Upvotes: 1
Reputation: 7884
It's a small issue I think. I believe this line:
move_uploaded_file($file_tmp,"/uploads".$file_name);
should be tweaked ever so slightly to:
move_uploaded_file($file_tmp,"/uploads/".$file_name);
because the filename (I would guess) does not have a leading forward slash.
Upvotes: 1