Edit File Image Upload using PHP - php

I've been having issues with file upload. After help on this site I managed to upload the files successfully to the db and can pull them back to show the images.Part of my assessment is the fact I need the ability to edit my photo choices (by say choosing a different image and maybe keeping one or two the same) and then the user can submit any changes they may wish to make.So on a separate file called 'edit.php' I have pre-populated all the other data that was entered in the db (this works fine) My code looks like:
<label for ="file">Filename:</label>
<?php echo "<img src='upload/".$_FILES['filename']."' />"; ?>
<input type="file" name="file[]" id="file" >
<?php echo "<img src='upload/".$_FILES['filename']."' />"; ?>
<input type="file" name="file[]" id="file" >
<?php echo "<img src='upload/".$_FILES['filename']."' />"; ?>
<input type="file" name="file[]" id="file" >
PHP:
$name = strtolower($name);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $name);
$extension = end($temp);
if(in_array($extension, $allowedExts))
{
if($_FILES['file']['size'][$f] < 2000000)
{
$uniqid = uniqid();
move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . $uniqid . "." . $extension);
mysql_query("INSERT INTO vehiclesimages (vehicleid, filename) VALUES (".$last_id.", '".$uniqid .".".$extension."')");
}
else
{
echo "File size to big!";
}
}
else
{
echo "Image not supported, please try again!";
}
Any ideas/help or useful sites would be greatly appreciated.As I have been unable to research any info on the subject.

Related

How to upload files and name them with file type?

hey guys so I created a website that you can upload books to and display them as a list I have a form to input the name of the book and the file type and I want them to display as the name and file type (on the same line) example (nameofabook epub) but when I try is display them it shows up like (nameofabook
new line epub) here's my code thank you
<?php
if (isset($_FILES['file']) && isset($_POST['name'])) {
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array("mov", "avi", "mp4", "epub", "pdf"); //The extensions you allow
if (in_array($file_ext, $allowed)) {
if ($file_error === 0) {
if ($file_size <= 2097152) { //the maximum filesize
$file_destination = ''.$file_name; // If ' ', the file will be placed in this directory
if (move_uploaded_file($file_tmp, $file_destination)) {
echo $file_destination;
$fp = fopen('book_list.txt', "a");
fwrite($fp, $_POST['name']. "|||" .$file_destination."\n");
fwrite($fp, $_POST['type']. "|||" .$file_destination."\n");
fclose($fp);
} else {
echo "An error has been encountered while moving your file!";
}
} else {
echo "Your file is too big!";
}
} else {
echo "An error has been encountered while uploading your file!";
}
} else {
echo "You can't upload files of this type!";
}
}
?>
if anyones curious heres my html for the upload page
<!DOCTYPE html>
<html>
<head>
<title>Upload a Book</title>
<link href = "style2.css" type = "text/css" rel = "stylesheet" />
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name" required/>
Type: <input type="text" name="type" required/>
File: <input type="file" name="file" required/>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Firstly...
Don't upload files directly in your site! You should upload them with an API, outside, in a cloud storage. In this case, you can use Cloudinary . So, you don't need to think much about the files uploaded. Again, you can show them easily!
Don't store information like names and others(without the file) in a text file. Storing them in a MySQL database is the best way to do so.
Secondly...
Use different inputs for collecting book names and other information so that, you can display and modify them easily.
Thirdly...
Please give the code you are using for displaying data
I'm a Homo sapiens So I can write anything wrong. Please forgive me for that.

Send file to server derictory over form with user's name

I have a form.html
<form action="user.php" method="post" enctype="multipart/form-data" >
<input type="text" size="40" name="UserCallFile" >
<input type="file" name="filename"><br>
<input type="submit" value="Send"><br>
</form>
And user.php
<?php
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_FILES["filename"]["name"]);
} else {
echo("Error");
}
?>
I need that user could download file to server which name of this file he written on text input. Ho to do that?
I tried this way
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_POST["fileName"] . "." . "png");
But of course here "png" have to be logic with any file extension.
I hope you are understand the idea of problem.
Simply try to play with this..
$image = $_FILES["filename"]["tmp_name"];
$dir = "gallery/"; //adjust this correctly, watch for slashes depending on your website config
$newname = $image['name'];
if(!#copy($image, $dir . $newname))
{
echo 'error';
}

uploading video file not working

I want to upload video files in php for that i am using following code
PHP
$newUploadDir = "c://video";
$idx = "file";
if (isset($_FILES[$idx]) && is_array($_FILES[$idx])) {
echo "file set";
foreach ($_FILES[$idx]["error"] as $key => $error) {
echo "loop";
if ($error == UPLOAD_ERR_OK) {
echo "<br/>dd2";
$tmp_name = $_FILES[$idx]["tmp_name"][$key];
$name = $_FILES[$idx]["name"][$key];
$ext1 = explode(".", $name);
$extension = end($ext1);
$newfilename = "test".".".$extension;
$video_types = array('mp4', 'avi','webm');
if (in_array($extension, $video_types)) {
if (move_uploaded_file($tmp_name, $newUploadDir.$newfilename)) {
echo "uploaded to folder";
} else {
echo "Not uploaded to folder";
}
}
} else {
echo "not uploaded $error";
}
}
}
echo "ok";
HTML
<form action="http://localhost/fileupload/video.php" enctype="multipart/form-data" method="post">
<input id="file1" name="file[]" type="file"/>
<input name="userId" type="text" value="2"/>
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>
Output
file setloopnot uploaded 1ok
Video file is not uploading. How to resolve this?
Actually, if you try to upload very large (video) files, probably the upload file size limit will not let you do that. Instead of regular file upload, there are other possibities. For the begining, look at this, this or this.
An other aproach would be to use third party services like Amazon S3, Microsoft Azure, etc.

php uploads and zero filesize

I would so appreciate some help here please. I have been struggling without success for a full day to upload images to a shared host running Zeus (rather than apache--yes I know, they are changing!). The host blames the code yet wont tell me why "as they are not programmers" I have tried so many different version of the form script I am out of options. I have of course checked the upload limits on the php ini file configs (which I am not allowed to change by the host) and they are both 128meg. So it looks unlikely that is the cause. The outcome of the scripts is that we get to the final ''successfully loaded the file'' message but the files size loaded is zero (so there is nothing new in the target directory). I am relatively new to php so please do go easy on the jargon. Thank you.
Here are the two files>> First the form...
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
and the php file refered to by the form...
<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size >350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to..";
echo $target;
echo "..The uploaded file size is $uploaded_size";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
First make sure the uploads folder exists and that is has write permissions set to either 755 or 777
Plus, you have two unassigned variables, $uploaded_size and $uploaded_type, so that will fail.
You would need to use something like if ($_FILES["file"]["size"] <350000) etc. probably another reason why it's failing.
Give this a try, see if this works for you, it's what I use:
Modify $allowedExts = array("gif", "jpeg", "jpg", "png"); for permitted file extensions.
It will upload only if size is less than 350000
NOTE: Change <input name="uploaded" type="file" /> to <input name="file" type="file" /> see form under handler code.
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 350000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Also change your form to this to reflect the PHP handler:
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" />
</form>

pass a text field to be a field in a linked php file in order to create a parameter

I have a form on a page which uploads a file. This form also has a 'ship_id' field to identify which record the file belongs to. Basically I need to pass the 'ship_id' field into 'upload_update.php' so that the link to 'update_ship.php' has two parameters, the name of the ship (which works perfectly well) AND the 'ship_id' which isn't working.... not sure how to achieve this.
Many thanks
Main form:
<form action="upload_update.php" method="post"
enctype="multipart/form-data">
<p>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input name="ship_id" type="text" value="<?php echo $_GET['ship_id']; ?>" />
<input type="submit" name="submit" value="Submit">
</p>
</form>
upload_update.php file:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Image succsefuly uploaded. " . "<br>" . "<br>";
?>
Click Hereto return to Add Ship page
<?php
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../images/ships/" . $_FILES["file"]["name"]);
}
}
}
else
{
echo "Invalid file";
}
?>
with $_GET['ship_id'] you get the field as a GET parameter.
Then you submit it in the form as a POST parameter. So in the upload_update.php you should get it with
$_POST['ship_id']
Also you might consider sending it in the form as a hidden field:
<input name="ship_id" type="hidden" value="<?php echo $_GET['ship_id']; ?>" />

Categories