i just moved from asp to php and i'm able to upload files to the server correclty. Now what i want to do is to rename the file before the upload is done and also echo the newly named file
<?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){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../complains_photos/".$file_name);
echo $file_type;
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
Just change the name that you are passing to the move_uploaded_file function. Something like this:
$destinationFileName = date('Ymd').'.'.$file_ext;
move_uploaded_file($file_tmp,"../complains_photos/".$destinationFileName);
Also change your echo to send $destinationFileName
Change move_uploaded_file(...); statement in the following way,
move_uploaded_file($file_tmp,"../complains_photos/".date("d-m-Y").".".$file_ext);
You can change the date format as per your preference.
Reference: http://php.net/manual/en/function.date.php
Related
I am trying to upload files. I am using xampp server. Everything is working perfect in localhost. Files are moving to specific folder. But in server file name is storing in database but not moving to folder. Please check my code
<?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){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
I gues the destination folder is not well defined.
Try to make the path absolute to check waht really happens.
move_uploaded_file($file_tmp, "/full/path/to/images/".$file_name);
For my website, I have a PHP image file upload system so that users can upload their own pictures. Those pictures are stored in /images/, and I have changed its file permission to 777 . When I test the upload file, it works but the file only keeps the .jpg extension and not the prefix:
<?php
$name= 'filename';
$newname= '/images/'. $name. '.jpg';
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){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
/*if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}*/
if(empty($errors)==true){
rename($file_tmp, $newname);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>`
I haven't actually seen rename before, but just trying it, I'll admit its behavior doesn't seem to be quite what I expect. Try move_uploaded_file instead.
Most importantly, I notice you were checking that the only image formats you accepted were png and jpeg, yet you were renaming them all to jpg. Why? Not all image viewers will open a png with a jpg extension.
<?php
$name= 'filename';
$newname= '/images/'. $name;//. '.jpg'; // why wouldn't you preserve the correct extension? i.e. $file_ext
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){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
/*if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}*/
if(empty($errors)==true){
// // // use move_uploaded_file() instead // // //
//rename($file_tmp, $newname);
move_uploaded_file($file_tmp, "$newname.$file_ext");
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
I've created a form in html, for upload image, it seems work perfect because when I upload the image it give me the string "Success", but i can't find the file in the server.
this is my code:
<?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){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"./photo/".$file_name);
echo "Success";
}
else{
print_r($errors);
}
}
?>
<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>
Check move_uploaded_file:
$ret_value = move_uploaded_file($file_tmp,"./photo/".$file_name);
if ($ret_value == false) die ("Ups! Couldn't actually move the temp file! No success at all.");
If it is returning false (error) the file could not be being actually moved from the temp file and you still would get the "Success" message, because no exception would arise.
If you're getting an error, maybe you can fix it by moving the file to the actual existing directory.
move_uploaded_file($file_tmp,"./poi/photo/".$file_name);
Your image upload is clearing every validation.
But, you are not storing it at correct paths:
Change:
move_uploaded_file($file_tmp,"./photo/".$file_name);
To:
move_uploaded_file($file_tmp,"./poi/photo/".$file_name);
Also, apply write permissions for the directory poi.
I have wrote a simple PDF uploader in PHP. It works fine and the PDF uploads fine. However I would like to print the file name and the file path (where it is being saved). On the upload.php form. Is this possible?
upload.php
<form action="../pdf/upload_pdf2.php" method="POST" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="image"/>
<label><?php echo strlen($filename ) > 0 ? $filename : "" ?> will be saved to /public_html/dev/pdf</label>
<p> </p>
<input type="submit" class="upload-button"/>
</form>
upload_pdf2.php
<?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'])));
$extensions = array("jpeg","jpg","png","pdf");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excatly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../pdf/".$file_name);
echo "File uploaded sucessfully";
}else{
print_r($errors);
}
}
?>
Is it possible to change the upload dir to some other in PHP other than htdocs folder? As I don't want my files to be exposed to web directly. Can it be done? I am using my Desktop as upload location, but files are not getting uploaded here! Here is my code:
<?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("txt");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"/home/abhi/Desktop/uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
Yes, its possible. The folder you save it into must have the correct permissions from the "user" running the webserver. Take a look at Can I find which user the web server is being run as in PHP?