I'm very unskilled at php so I'm afraid I have no code to provide, but I don't think what I need is difficult to make anyway.
I need to create a form with two inputs, a "Name" field and a browse for file field, that will upload an image onto a folder in my server and rename that image to whatever was in the "name" field. Any help would be much appreciated, thank you!
you can use the following code
html file upload form
<form name="fileUpload" action="upload.php" method="post" enctype="multipart/form-data">
<input type="text" id="name" name="name" maxlength="40" required data-validation-required-message="Please select a name.">
<input type="file" id="file" name="file_name" maxlength="40" required data-validation-required-message="Please select a photo.">
<input type="submit" value="UPLOAD PHOTO"/>
</form>
Now your upload.php file should look like this.
<?php
$album_name = filter_input(INPUT_POST, 'name');
$oldfileName = $_FILES["file_name"]["name"]; // The file name
move_uploaded_file($fileTmpLoc, "your upload file path" .$fileName);
$newfileName = "new name";
rename ( "your upload file path/" .$oldfileName$ , "your upload file path/" .$newfileName )
?>
Related
So, I used a form to upload files to a directory.
The PHP code is as follows:
if(isset($_POST["submit"]))
{
move_uploaded_file($_FILES['cvfile1']['tmp_name'], '/uploads/'.$oor1 . '_CV_' . $firstname . '_' . $lastname);
}
But when I execute it, it seems to not place the file anywhere. I have been refreshing the uploads directory for a while, hoping it would show up.
The variables have been declared and pulled off from the form
You can use multiple file upload
<input type="file" name="img" multiple>
OR
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
I am experiencing an error with the following code whereby the file fails to upload to the server. Can someone please take a look at the two files and advise where I have got it wrong, I know I have probably been thick but any help would be greatly appreciated.
The files are on a small internal intranet so I am not concerned with the fact I am putting the database connection and password into the file.
As I said any help would be greatly appreciated as I am only receiving the final sorry error and no information is being passed to the MySQL database. I can get the form and code to work fine without the upload but not with.
I have checked the upload directory and permissions for access to it and all are ok.
the html form:
<form enctype="multipart/form-data" action="add.php" method="POST">
Model:<br /> <input type="text" name="model"><br>
Size:<br /> <input type="text" name="size"><br>
Total width:<br /> <input type="text" name="tot_width"><br>
Internal width:<br /> <input type="text" name="int_width"><br>
Total height:<br /> <input type="text" name="tot_height"><br>
Frame height:<br /> <input type="text" name="fra_height"><br>
Total depth:<br /> <input type="text" name="tot_depth"><br>
Seat height:<br /> <input type="text" name="sea_height"><br>
Seat depth:<br /> <input type="text" name="sea_depth"><br>
Arm height:<br /> <input type="text" name="arm_height"><br>
Std leg height:<br /> <input type="text" name="std_leg_height"><br>
Image:<br /> <input type="file" name="image"><br>
<input type="submit" value="Add">
</form>
The PHP:
//This is the directory where images will be saved
$target = "/images/product";
$target = $target . basename( $_FILES['image']['name']);
//Get all the other information from the form
$model=$_POST['model'];
$size=$_POST['size'];
$totWidth=$_POST['tot_width'];
$intWidth=$_POST['int_width'];
$totHeight=$_POST['tot_height'];
$fraHeight=$_POST['fra_height'];
$totDepth=$_POST['tot_depth'];
$seaHeight=$_POST['sea_height'];
$seaDepth=$_POST['sea_depth'];
$armHeight=$_POST['arm_height'];
$stdLegHeight=$_POST['std_leg_height'];
$pic=($_FILES['image']['name']);
// Connect to Database
mysql_connect("localhost", "databaseUser", "databasePassword") or die(mysql_error()) ;
mysql_select_db("databaseName") or die(mysql_error()) ;
//Write information to the database
mysql_query("INSERT INTO `prod_dims` VALUES '$model', '$size', '$totWidth', '$intWidth', '$totHeight', '$fraHeight', '$totDepth', '$seaHeight', '$seaDepth', '$armHeight', '$stdLegHeight', '$pic'") ;
//Put the image on the server
if(move_uploaded_file($_FILES['image']['name'], $target))
{
//Is the upload ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Give an error if its not
echo "Sorry, there was a problem uploading your file.";
}
change
if(move_uploaded_file($_FILES['image']['name'], $target))
to
if(move_uploaded_file($_FILES["image"]["tmp_name"], $target))
I have a Form with some fields:
<form action="xyz.php" method="post" enctype="multipart/form-data">
<input type="text" placeholder="First name" name="firstname">
...
<input type="file" name="logo">
...
<input type="submit" name="submit">
</form>
The image should be uploaded immediately to img_upload.php. In this file i need the $_FILES array.
My img_upload.php script uploads the image, do some things with the image (resize, ...) and give me the URL to the image. After this, the image should be displayed in the form.
Is there any chance to upload the image (send $_FILES array to another file) without submiting the whole form?
<input type="file" name="logo" onchange="uploadThisFile(this)" >
function uploadThisFile(file) {
/* launch an ajax request to img_upload.php and pass file details via POST */
}
I'm struggling on letting the user upload and image to an image field on the HTML doc. When i test it, i can select which image to upload and then click "UPLOAD" but it doesn't go anywhere or go to the field i want to designate it to.
<input name="imgfield" type="image" width="100" height="100">
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<label for="file">Select Picture</label>
<input type="file" name="file" id="file" />
<input type="submit" name="btn_uploadpic" id="btn_uploadpic" value="UPLOAD"
<?php
if (isset($_POST['btn_uplaodpic']))
{
$id=$_POST['imgfield'];
}
?>/>
</p>
</form>
When uploading a file, you won't get the uploaded file in $_POST, but in $_FILES. Check here for a quite detailed example on how to upload files with PHP:
http://www.w3schools.com/php/php_file_upload.asp
You have to refer to a temporary filename, using files, you can do something like
$_FILES['imgfield']['tmp_name']; // temp_name can be anything
If referring to 'name' isn't enough then give the input an 'id' of value 'imgfield'
I succeeded in storing all the image information as a blob in MySQL via the form tag and php.
Now I'm trying to make an update form using PHP5. However, I'm not sure how to take all the information back from MySQL and show it to users that an image has been already posted.. like any other typical forum / blog pages that shows previously added files.
Any suggestions..? Thank you.
<form method="post" enctype="multipart/form-data" action="UpdateNewsPHP.php">
Title : <input name='TitleFieldToAdd' type='text' size='20' value='<?php echo $row["Title"] ?>'/> <br/>
Thread : <textarea name='ThreadFieldToAdd' cols="40" rows="10"><?php echo $row["Thread"] ?></textarea> <br/>
<!-- Here I have no clue how to deal with them... :( -->
Image : <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input type="file" id='ImageFieldToAdd' name="files[]" /> <br/>
<input id="submit" type="submit" name="submit" value="Upload me!">
and this is the information I store in MySQL
$title = $_REQUEST['TitleFieldToAdd'];
$thread = $_REQUEST['ThreadFieldToAdd'];
$file_content = file_get_contents($_FILES['files']['tmp_name'][0]);
$file_content = mysql_real_escape_string($file_content);
$file_name = $_FILES['files']['name'][0];
$file_size = $_FILES['files']['size'][0];
$file_type = $_FILES['files']['type'][0];
$datePosted = date("Y-m-d");*/
The proper way is to leave your file upload stuff asis. If a file has already been uploaded, you display it another section of the form. Imgs can be embedded with an <img> tag (pointing at another script which retrieves/serves up the raw image data from the database). Non-displayable fields (pdf, zip, etc..) you can just put in a direct download link.
Then your form will look something like
[input1]
[input2]
You previously uploaded: [link/image to uploaded data]
[input file] - would you like to replace this data?
As such, your form building script would not actually retrieve the uploaded file data. Just its metadata (type/size).