luigimia
-
Posts
12 -
Joined
-
Last visited
Never
Posts posted by luigimia
-
-
Hi. I have a script here that will let users upload an image to my website but I just can't figure out how to save the uploaded image as "upload/logo.png" so that it will replace the already existing "upload/logo.png".
Help would be greatly appreciated.
<html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?>
Thanks!
-
include('../../yourfile.php');
Thanks wigpip! It worked! Thanks :L
-
How do I include a file that is located two directories above the file that I want to include the file into?
My absolute hosting path is /home/content/52/8840652/html if this would be of any use. I'm not quite sure about the PHP include functions.
Any help would be greatly appreciated,
Thanks!
-
Hi.
I'm making a website where users can upload files along with title, description and image.
I can get them making the page (str replace) but I only know how to list the files with just the file title in a basic links directory.
Any ideas how I could include title, description and a picture (as well as the link to the page). Sort of like with Youtube search results or engine001.com/games.htm?
Any help would be greatly appreciated.
Thanks
are you using a database to store this information?
a little more information on the subject would be helpful.
I'm not. Basically I have a file upload page and on that page there's the title, description, image and file fields.
When a user clicks submit a second page, process.php, uploads the file and image and puts the file into the files directory, the image into the pictures directory. Then, using a default template and str replace, the description, title and username are filled out. The picture is sourced from the picture directory and the download link from the files directory (again using str_replace).
Although I do have a MySQL for the users I'm not using a database for the files. Only the three php pages, upload, process and template.
Any ideas?
Thanks!
-
Hi.
I'm making a website where users can upload files along with title, description and image.
I can get them making the page (str replace) but I only know how to list the files with just the file title in a basic links directory.
Any ideas how I could include title, description and a picture (as well as the link to the page). Sort of like with Youtube search results or engine001.com/games.htm?
Any help would be greatly appreciated.
Thanks
-
There seems to be items missing from that tutorial.
Don't foget to have a folder named upload in the same directory as this script is, or change your target path.
pretty sure I added the essentials to this, also included a timestamp to the front of the file name so you don't have duplicate named files.
<html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?>
You can use the file types with if/else or a switch statement and display a resized image if was an image, a link if was a file, an embed if audio or video, etc....
I just made it a hyperlink for simplicity.
You are a life saver. Thank you very very very much.
-
Some changes to the previous code, also added checking for extensions within the allowed mime types.
<html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed"; } } } else { echo "Select your file to upload."; } ?>
Thanks! Quick question though, where do I put your script in proportion to the original? I tried mingling it in but must have put it in the wrong place because some times I got it being treated as two different scripts and sometimes being presented with an error?
Where do I put the original?
Thanks!
-
I can't get this to work?
<?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/php”) { echo “No PHP files”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/cgi”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/html”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/asp”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/pl”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/gif”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/jpg”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/png”) { echo “Not an approved file type.”; $ok=0; } if (isset($uploaded_type) && $uploaded_type ==”text/tif”) { echo “Not an approved file type.”; $ok=0; } if ($ok==0) { Echo “Sorry your file was not uploaded”; } else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo “The file “. basename( $_FILES['uploaded']['name']). ” has been uploaded”; } else { echo “Sorry, there was a problem uploading your file.”; } } ?>
-
you will want to the compare the mime of the file to the $_FILES[filename][type] value.
I like to store the valid extensions in an array and compare the array of values to the mime type of the given file using a conditional statement.
May you explain how to do that?
-
Hi. I'm making a file-sharing website but how do I stop users from uploading certain extensions?
Here is my script so far:
session_start();
$file_name = $HTTP_POST_FILES['ufile']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.$file_name;
$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";
}
else
{
echo "Error";
}
}
?>
-
Hi.
I want to create a list of files in a directory. But I don't want all the files to be listed, only .gif, .jpg and .bmp.
Does anyone know the PHP code how to do that? (My directory name is 'images' if you need that).
Thanks!
Replace image with an uploaded one?
in PHP Coding Help
Posted
I tried but just kept on getting "There was a problem saving the file". Could someone please show me how to do it?
Thanks again.