When I upload a image file
I can't see anything in tmp folder of my wamp server
When I execute move_upload_file function, no file has been uploaded in my destination folder.
if(isset($_POST['submit'])){
$file = $_FILES['file'];
$ext = explode('.',$file['name']);
$fileActualExt = strtolower(end($ext));
$allowed = array('jpg','jpeg','png','pdf');
if(in_array($fileActualExt,$allowed)){
if($file['error']===0){
if($file['size']<50000){
$fileNameNew = uniqid('',true).'.'.$fileActualExt;
$fileDestination = '\uploads\\'.$fileNameNew;
move_uploaded_file($file['tpm_name'],$fileDestination);
header('location:index.php?uploadsuccess');
}else{
echo 'size too big';
}
}
}else{
echo 'extension incorrect';
}
}
This works on Wamp 3+.
Go to wamp folder (wamp/ or wamp64/)
Open wampmanager.conf
Find urlAddLocalhost param and set it on: urlAddLocalhost = "on"
There should not be the need to tweak the index.php in www folder.
Related
I try using function to upload different kind of file by giving it variables. As shown below:
<?
function fn_fileUpload($data,$dir,$uid){
include_once($_SERVER['DOCUMENT_ROOT']."/cgi-bin/connect.php");
if(isset($data) && $data['error'] === UPLOAD_ERR_OK){
$fileTmpPath = $data['tmp_name'];
$fileName = $data['name'];
$fileSize = $data['size'];
$fileType = $data['type'];
$fileNameCmps = explode(".", $fileName);
$fileExt = strtolower(end($fileNameCmps));
$newFileName = $uid . '.' . $fileExt;
//check file ext
$okEXT = array('jpg', 'jpeg', 'png','doc','docx','pdf');
if (in_array($fileExt, $okEXT)) {
$fileDir = '/'.$dir.'/';
$dest_path = $fileDir.$newFileName;
if(move_uploaded_file($fileTmpPath, $dest_path)){
try{
$stmt2=$mysqli->prepare("insert into job_file (jfile_id, job_id, jfile_name, jfile_size, jfile_type, jfile_ext) valies(?,?,?,?,?,?)");
$stmt2->bind_param('iisiss',$zero,$uid,$newFileName,$fileSize,$fileType,$fileExt);
$stmt2->execute();
$result = 'ok';
}catch(mysqli_sql_exception $err){
$result=$err;
}
}else{
$result = 'Cannot upload file!';
}
}//in_array
}//if(isset
return $result;
}
?>
And this is how to use:
//upload file
$job_file=fn_fileUpload($_FILES['job_file'],'uploads',$_POST['passport_id']);
//upload photo
$job_img=fn_fileUpload($_FILES['job_img'],'photos',$_POST['passport_id']);
From here, the function always return : Cannot upload file!. At first I think. It might have something to do with move_uploaded_file but the file was there in /uploads directory but not with /photos. Both directories CHMOD 755 (tried 777 but no luck).
The db went through correctly. Is there any idea how to fix this?
You can only use move_uploaded_file() ONCE on a temporary file that has been uploaded through a form. This function destroys the temporary file after it has been moved, so the for the first upload in the uploads directory you can do it well but in for the second one no.
This question already has an answer here:
Permission denied while uploading a file
(1 answer)
Closed 7 years ago.
<?php
include_once 'database/dbconnect.php';
if(isset($_POST['btn-upload'])){
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$sql="INSERT INTO upload(file,type,size) VALUES('$file','$file_type','$file_size')";
mysql_query($sql);
}
?>
<?php
if( $_FILES['file']['name'] != "" ){
copy( $_FILES['file']['name'], "uploads/" ) or
die( "Could not copy file!");
}
else{
die("No file specified!");
}
?>
When I try to upload any file using xampp, I'm facing an error which says
Warning: copy(first.pdf): failed to open stream: No such file or directory in /opt/lampp/htdocs/new-project/upload.php on line 4
Could not copy file!
I even tried to change the folder permission by
sudo chmod -R 755 /opt/lampp/htdocs/new-project/
But, nothing as changed.
Configure The "php.ini" File
First, ensure that PHP is configured to allow file uploads.
In your "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
Since name is the original name and not the path to the uploaded file you should use tmp_name instead:
// check so file is set and contains no error
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$destination = 'uploads/' . $_FILES['file']['name'];
// use move_uploaded_file() instead of copy()
move_uploaded_file($_FILES['file']['tmp_name'], $destination);
}
else {
die("No file specified!");
}
If error is not equal to 0 you can check what kind of error occured following this link: File upload errors
I can insert the folder path into the database but the picture file doesn't seem to move into that folder?
$pic = $_POST['pic'];
$picName= $_FILES['pic']['name'];
$type = $_FILES['pic']['type'];
$tmp = $_FILES['pic']['tmp'];
$picPath = "/pictures/";
if(is_uploaded_file($tmp)) {
if(move_uploaded_file($tmp, $picPath . $picName)) {
echo "congrats! Image is uploaded.";
}
else {
echo "Sorry, couldn't move your picture.";
}
}
else {
echo "Sorry, couldn't upload your picture.";
}
$picPath = $picPath . $picName;
mysql_query("INSERT INTO User(pic) VALUES ('$picPath')");
I get this echo message: Sorry, couldn't upload your picture.
The php files is saved on public_html folder, and I have a pictures folder where I want to move the users pictures into.
The insertion works as I can store the $picPath in my database, but the picture don't get stored in my folder.
Try replacing
$tmp = $_FILES['pic']['tmp'];
with
$tmp = $_FILES['pic']['tmp_name'];
1) check folder permissions if its writable or not.
2) make sure your path is same with the same folder name in code as well.
3) Try to change from this $picPath = "/pictures/"; to something like this $picPath = "pictures/"; removed forward slash.
I am trying to use Uploadify on my site and have it setup with the following uploadify.php:
<?php
// Define a destination
//$targetFolder = '/uploads'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'];
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
It wasn't uploading so I commented out $targetFolder and changed $targetPath so that it is just the document root. This results in $targetFile being /home/user/public_html/example.com/file.txt when I try to upload file.txt with Uploadify. The folder is set to 755. I'm at a loss as to what the problem could be. I am otherwise using a vanilla install of Uploadify and everything seems to work fine except that the file never actually goes where it should.
It was an issue with the folder permissions not staying set to 755. I logged out of and back into cPanel and everything's working now.
Dear friends, this is a script which simply upload file and insert filename into database, why is this not working ? It's just upload the file and send filename to db even after validation . Please help
<?php
//file validation starts
//split filename into array and substract full stop from the last part
$tmp = explode('.', $_FILES['photo']['name']);
$fileext= $tmp[count($tmp)-1];
//read the extension of the file that was uploaded
$allowedexts = array("png");
if(in_array($fileext, $allowedexts)){
return true;
}else{
$form_error= "Upload file was not supported<br />";
header('Location: apply.php?form_error=' .urlencode($form_error));
}
//file validation ends
//upload dir for pics
$uploaddir = './uploads/';
//upload file in folder
$uploadfile = $uploaddir. basename($_FILES['photo']['name']);
//insert filename in mysql db
$upload_filename = basename($_FILES['photo']['name']);
//upload the file now
move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);
// $photo value is goin to db
$photo = $upload_filename;
function send_error($error = 'Unknown error accured')
{
header('Location: apply.php?form_error=' .urlencode($error));
exit; //!!!!!!
}
//file validation starts
//split filename into array and substract full stop from the last part
$fileext = end(explode('.', $_FILES['photo']['name'])); //Ricky Dang | end()
//read the extension of the file that was uploaded
$allowedexts = array("png");
if(!in_array($fileext, $allowedexts))
{
}
//upload dir for pics
$uploaddir = './uploads/';
if(!is_dir($uploaddir))
{
send_error("Upload Directory Error");
}
//upload file in folder
$uploadfile = $uploaddir. basename($_FILES['photo']['name']);
if(!file_exists($uploadfile ))
{
send_error("File already exists!");
}
//insert filename in mysql db
$upload_filename = basename($_FILES['photo']['name']);
//upload the file now
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile))
{
send_error('Upload Failed, cannot move file!');
}
// $photo value is goin to db
$photo = $upload_filename;
This is a cleared up version to yours, give that a go and see if you get any errors
You can find the extension of file by using this code also.
$tmp = end(explode('.', $_FILES['photo']['name']));
now $tmp got the extension of file.
Why not use PHP's built-in functions to extract the extension from the filename?
$fileext = pathinfo($_FILES['photo']['name'],PATHINFO_EXTENSION);
And if the file extension is valid, you're returning from the function without doing anything further, if it's invalid you're setting the header, but the code logic will continue to your file processing
You blindly assume the file upload succeeded, but there's many reasons for it to fail, which is why PHP provides ['error'] in the $_FILES array:
if ($_FILES['photo']['error'] === UPLOAD_ERR_OK) {
// uploaded properly, handle it here...
} else {
die("File upload error, code #" . $_FILES['photo']['error']);
}
The error codes are defined here.