I have a code where I am selecting an image and then entering the name of the directory I want to create to save that image. After this, once a button is clicked the directory should be created in the given path and file should be saved in it.
Below is the code I am using:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file"/>
<input type="text" name="idtest" value=<?php echo $idtest; ?> >
<input type="submit" value="Send File" multiple/>
</form>
upload.php
<?php
$uploaddir = 'G:/dataset/' . $idtest;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;
echo $uploadfile;
?>
Here in the above code I have not include the create directory part which I still have to work on. Now ideally the variable idtest should be appended with uploaddir variable so whenever I am printing the value of uploadfile the last directory should be the one which I entered in the text box. But its not working. Can anyone please throw some light on why its not working. Thanks
Give this upload script a try. Based on your comments, it seems the first issue is the directory name entered in the form not being added to the full uploadfile path. You will need to grab idtest from the $_POST variable first. I would also recommend using move_uploaded_file to move the uploaded file.
upload.php
<?php
$uploaddir = 'G:/dataset/' . $_POST['idtest'];
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir ."/". basename($_FILES['userfile']['name']) ;
echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile);
?>
Please ensure proper form validation and file name checks for security reasons, but that is beyond the scope of this answer. As the other commenters have mentioned, ensure file system permissions are set to allow writing to G:/dataset/.
Related
i'm trying to save an image uploaded with a HTML form on my localhost server (working with Xampp), but although there are no errors, the file isn't saved anywhere.
This is the form, really simple:
<form id="form1" action="result.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="imguploaded" id="imguploaded" accept=".png, .jpg, .jpeg"></br>
<input class="btn btn-outline-danger btn-lg" id="inputbtn" type="submit" value="Upload Image" name="submit">
</form>
and this is the PHP code (on result.php):
<?php
$result=false;
$error=false;
if (isset($_FILES['imguploaded'])){
$nomefile = strtolower($_FILES['imguploaded']['name']);
$path = "caricamenti/$nomefile";
move_uploaded_file($_FILES['imguploaded']['name'], $path);
echo($path);
}
?>
the path exists and it is in the same folder of the .PHP files.
In move_uploaded_file($_FILES['imguploaded']['name'], $path); the first parameter isn't correct. It should be the temporary path where php stored it intermediatly, which you find in $_FILE['imguploaded']['tmp_name'].
So change that line to
move_uploaded_file($_FILES['imguploaded']['tmp_name'], $path);
relevant docs
Be sure to:
sanitize filename & extention first
check for allowed mimetypes, size, ..
Right now I could easily upload a php script and execute that.
Try this:
$file_name= $_FILES['imguploaded']['name'];
$file_tmp_name= $_FILES['imguploaded']['tmp_name'];
$div= explode('.',$file_name );
$file_txt= strtolower(end($div));
$unique_image= substr(md5(time()),0,10).".".$file_txt;
$uploaded_image= "caricamenti/".$unique_image;
move_uploaded_file($file_tmp_name,$uploaded_image);
I'm attempting to build my first form/file uploader (I'm a newb fyi).
I am testing on a local server on my mac, both the form, file handler, and the uploads folder are in the same file directory with one another.
When I select a file using the submit form (test file is 'testFilego.txt' and 3 bytes in size'), i get the following error: http://localhost/PhP_exercises/__tizag/280-php-fileupload-test.php?MAX_FILE_SIZE=2500000&uploadedfile=testFilego.txt. The submit form doesn't seem to connect to the handler (?). I thought the test file would appear in my uploads folder. Help.
This is the submit form:
<form>
<form enctype="multipart/form-data" action="280-php-uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2500000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</form>
This is the file hander the form ought to be contacting
<?php
280-php-uploader.php<?php
//This is '280-php-uploader.php'
// Where the file is going to be placed
$target_path = "uploads/";
// Add the original filename to our target path.
//Result is "uploads/filename.extension"
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
As Marco Acierno observed, i'd posted the form inside a form and that seems to have caused the problem.
I am unable to successfully upload an image/file to my server. The php is as follows:
//This is the directory where images will be saved
$uploadDir = "./";
$uploadFile = $uploadDir . basename( $_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile)){
echo "The file has been uploaded successfully.";
} else {
print_r($_FILES);
}
I chose the directory at which this script lives, to ensure the functionality before I upload to the final directory. I want to upload photo's, and will check for file extensions later - but for now I at least need the upload functionality to work.
I get an empty array returned.
The form is as follows:
<form id="imageUploadForm" name="imageForm" enctype="multipart/form-data" action="imageController.php">
<label for="photo" class="blogLabel">Upload an Image</label>
<input type="file" name="photo" id="imageUpload">
<input type="submit" name="submit" id="imageSubmit" class="btn btn-primary" value="Upload">
</form>
You forgot the most important thing about a form -- the method!
If there is no method set it defaults to get.
You want post!
Add method="post" to your form.
Let me explain what i am hope to acomplish:
I want to allow my users to upload a image as avatar.
I found many php upload tutorials but i don't know hoy to upload the avatars as user_id.ext in /avatars folder.
I hope i was clear, thanks.
In any upload script, you go through a few basic steps. First, you get data from $_FILES telling you where the temporary upload file is. You validate the file based on something to make sure it's not evil/malicious/wrong. Then you rename it and move it somewhere useful. In your last step, when you move the image to where it's going, take that opportunity to name the file as you like. If you're dealing with a user's account it should be trivial to get the username, id, middle name, etc and use that to set the file's name.
This script gets the uploaded file and save it as /avatars/$user_id.ext, $user_id retrieved from POST:
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "/avatar/{$_POST['user_id']}.ext");
echo "Stored in: " . "/avatar/{$_POST['user_id']}.ext";
}
?>
And this is the form:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="hidden" name="user_id" value="<?php echo $user_id ?>">
<input type="submit" value="submit"></form>
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.";
}}
?>