PHP Upload and overwrite photo file - php

I'm working with some pretty plain PHP.
User upload of photo files with some destination and same name, so that the newest upload will overwrite older versions.
Right now my code seems to work. I get the upload comment I should, but no files in destination folder.
Ive been searching stackoverflow, but I just cant seem to figure it out.
HTML:
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
PHP:
<?php
$uploaddir = 'profilepicture/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

Make sure your MAX_FILE_UPLOAD_SIZE has been set to somewhat bigger than 2BM (default seetings in php.ini). To my experience, I set it to 20MB so for image that is bigger than 2BM will also be uploaded

what's platform that you work with ? if it's unix, check "profilepicture" permission and right relative path.

Related

move_uploaded_file returns error code 0 but file not uploaded in my directory?

I'm working on a project using codeigniter 3.0.6 and I need to upload image in some modules. the problem is that move_uploaded_file returns error code 0 but the file is nowhere to be found. I'm currently running this code on my localhost. if only it returns an error code then I can do something.. can anyone help me with this?
I've read Move_uploaded_file() function is not working too and my code below is based on one of the supposedly "working example" answer.. but still not working (error code 0 but file not found in directory). the mkdir($uploaddir, 0777, true); part doesn't seem to help.
this is my view file (display.php) :
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form enctype="multipart/form-data" action="submit" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
</html>
and this is my controller file (test.php) :
function display()
{
$this->load->view('display');
}
function submit() {
$uploaddir = '/assets/img/guide/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (!file_exists($uploaddir)) {
mkdir($uploaddir, 0777, true);
}
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Error code:';
print_r($_FILES['userfile']['error']);
print "</pre>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
echo $this->load->view('display', $this->data);
}
and this is the result after I submit the form:
File is valid, and was successfully uploaded.
Error code:0
Here is some more debugging info:Array (
[userfile] => Array
(
[name] => Hydrangeas.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php46FD.tmp
[error] => 0
[size] => 595284
)
)
it turns out the files WERE uploaded but instead of going to my project's base folder (D:/myproject/dev/), the files were uploaded to the harddrive folder (D:/) so i guess the mistery's solved. I just need to figure out how to direct the target folder for upload.. maybe I'll put it in another question post.
thank you all

Problems with a php form upload

I am trying to upload a file via a form and a php file. I have used the same method many times throughout my website with no problems, however this time I just can not get it to work. On my form I have this....
<span class="purple"><strong>Upload Your Image</strong></span>
<input name="userfile" type="file" id="userfile" class="textbox">
<br /><br />
<label>
<input name="submit" type="submit" class="createbutton" id="submit1" value="ADD TO BASKET">
</label>
In my php file I have this...
//upload image first
$uploaddir = '../images/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$status = 1;//uploaded
$data["printfile"] = $_FILES['userfile']['name'];
} else {
$status = 0;//cant upload
echo "Upload Failed!\n";
$err = "";
}
Every time it just gives me the Upload Failed error message. Any ideas?
Ah right, I really feel like a fool now. I somehow managed to miss enctype="multipart/form-data" on my form tag. Thanks so much for your help, works perfectly now.

Having trouble getting php file uploader to work

I'm having trouble getting a simple php file upload script to work. What happens is I get the Thanks message on my browser, but the file isn't anywhere to be seen.
I've been watching the upload directory and /tmp with inotify to see if anything gets created all but nothing does. Here is the html div that allows selecting the file:
<div>
<p> Select a file </p>
<form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="file">Filename:</label>
<input type="file" name="logfile" id="file"><br/><br/>
<input type="submit" name="submit" value="Upload">
</form>
</div>
So then I try and upload the file:
$uploadDir = "/tmp/";
if(isset($_FILES['logfile']['name'])){
$uploadFile = $uploadDir . basename($_FILES['logfile']['name']);
print "<p>{$_FILES['logfile']['name']}</p>\n";
print "<p>{$uploadFile}</p>\n";
# Delete if already exists
if (file_exists($uploadFile)) {
unlink($uploadFile);
}
// Recieve the file
if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
displayMessage("Thanks");
}
else {
# Couldnt move the file
displayMessage("Error moving file");
}
}
What happens is I get the message "Thanks" in my browser but no file. I've checked permissions, checked my nginx log - no errors at all. It looks like it is successful but nothing is being copied?
Edit:
Even this fails:
$myfile = fopen("/tmp/testfile.txt", "w") or die("Unable to open file!");
$current = "John Smith\n";
fwrite($myfile, $current);
fclose($myfile);
Basically I cant write to /tmp for some reason, even though nginx is not reporting any error. I've also tried the same script under apache, with the same results.
drwxrwxrwt 18 root root 460 Aug 20 10:56 /tmp/
It's working fine for me. can you check the upload directory? where you have kept your tmp folder? just use if the same place of the file $uploadDir="tmp/";
<?php
$uploadDir = "";
if(isset($_FILES['logfile']['name'])){
$uploadFile = $uploadDir . basename($_FILES['logfile']['name']);
print "<p>{$_FILES['logfile']['name']}</p>\n";
print "<p>{$uploadFile}</p>\n";
# Delete if already exists
if (file_exists($uploadFile)) {
unlink($uploadFile);
}
// Recieve the file
if (move_uploaded_file($_FILES['logfile']['tmp_name'], $uploadFile)) {
echo "Thanks";
}
else {
# Couldnt move the file
echo "Error moving file";
}
}
?>
<div>
<p> Select a file </p>
<form id="support" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="file">Filename:</label>
<input type="file" name="logfile" id="file"><br/><br/>
<input type="submit" name="submit" value="Upload">
</form>
</div>

How to add simple image upload functionality to WP plugin

I want to add simple image upload functionality to my WP plugin. So the simple form with upload button. I dont want to use standard thickbox included in WP for this.
When you press the button file selection dialog will appear, you select file from your drive and it'll be added to the input box.
Then when you press "save" button for plugin options it will send with form and handled on server side.
I wonder if there is ready to use WP functionality for the server part.
As I want to save the upload image path also to DB options table.
EDIT: added PHP tag as wordpress is a PHP language
You're in some luck-I'm currently working on a mod that would allow image uploads for MarketPress. Here's my skeleton, poorly indented script. This should help get you started methinks?
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10240" />
Send this file: <input name="userfile" type="file" />
Send this file: <input name="userfile2" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
if ($_POST['MAX_FILE_SIZE'] == '10240' ){
$uploaddir = 'public_html/uploads';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$validfiletype;
if ( preg_match('/\\.(exe|com|bat|zip|doc|txt)$/i', $_FILES['userfile']['name']) )
$validfiletype = 0;
elseif( preg_match('/\\.(jpg|jpeg|gif|png|pdf|psd)$/i', $_FILES['userfile']['name']) )
$validfiletype = 1;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File upload unsuccessful.";
}}
?>

error uploading file via php

I am using this script to upload files. I am not perfoming any check on filetype since its the first time I am trying. I am using Ubuntu and in php.ini file file upload is set to 'on'. But still I am not able to upload file.
<?php
if(isset($_POST['send']))
{
$uploaddir = "/home/harbhag/Desktop/";
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo $uploadfile;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
}
?>
<html>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="hidden" name="send" value="send" />
<input type="submit" value="Send File" />
</form>
</html>
You should use $uploaddire and $uploadfile both variables to make a valid path for file.
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$uploadfile)) {
And also note the points
Destination directory should have write permissions
Size of File to be uploaded must meet php's upload_max_filesize (default value is 2MB )limit
Be extra careful when using move_uploaded_file. It will not move files between partitios, and hostng companies may have their upload temp directory elsewhere tha your php files.
It's a safer choice to use:
$tempname = $_FILES['userfile']['tmp_name'];
if (is_uploaded_file($tempname)) {
copy($tempname, $uploadfile);
}

Categories