I built a file upload form in PHP, IT WORKS fine when it comes to images, but it will not upload an audio file.
I can upload all (most) other files, but can't upload mp4/mp3/wav
It registers as no file for some reason (it won't even echo out the file information like it does with images and other files) My code works just well for other types of files.
I have searched for a solution before posting this.
The code is below and you may try it.
Here is the code:
<?php
if (isset($_FILES['UploadFileField'])){
$UploadName = $_FILES['UploadFileField']['name'];
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadType = $_FILES['UploadFileField']['type'];
$FileSize = $_FILES['UploadFileField']['size'];
$UploadName = preg_replace("#[^a-z0-9.]#","",$UploadName);
echo $UploadName."</br>";
echo $UploadTmp."</br>";
echo $UploadType."</br>";
echo $FileSize."</br>";
if ($FileSize > 100000){
die("Error - File Too Big");
}
if(!$UploadTmp){
die("No File Selected, Please Upload Again");
}else{
move_uploaded_file($UploadTmp,"Uploads/$UploadName");
}
}
?>
<!Doctype HTML>
<html>
<head>
<style>
.fileuploadholder{
width:400px;
height:200px;
margin: 60px auto 0px auto;
background-color:#FFF;
border: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="fileuploadholder">
<label for="UploadFileField"></label>
<form action="FileUpload.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<input type="file" name="UploadFileField" id="UploadFileField"/>
<input type="submit" name="UploadButton" value="Upload"/>
</form>
</div>
</body>
</html>
Related
I try to modify the php scrip to allow it to upload pdf instead of image. I've done several modification but none of them are working. I really appreciate if someone can show me how to modify this script to allow uploading the pdf.
I'm very new in php and need more guidance from php experts out there.
Thanks in advance for your kind assistance.
Anyway this is the code:
## Heading ##<html>
<head>
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-
ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
#uploader {
width: 300px;
height: 200px;
background: rgba(0,0,0,.075) url(../../img/pdf_icon.png) repeat fixed left;
border-radius:8px;
box-shadow: 5px 5px 10px 0px rgba(0,0,0,0.15);
display: block;
padding: 10px;
}
#uploader.highlight {
background:#ff0;
}
#uploader.disabled {
background:#aaa;
}
</style>
<script src="drag-drop-upload-pdf.js"></script>
</head>
<body>
<!-- DROP ZONE -->
<div id="uploader">
Muatnaik fail pdf di sini...
</div>
<!-- STATUS -->
<div id="upstat"></div>
<!-- FALLBACK -->
<form action="upload-pdf.php" method="post" enctype="multipart/form-data">
<br />
<input class="btn btn-primary" type="file" name="file-upload" id="file-upload" accept="pdf/*">
<br />
<br />
<input class="btn btn-primary" type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
## PHP Script ##
<?php
// SOURCE + DESTINATION
$source = $_FILES["file-upload"]["tmp_name"];
$destination = __DIR__.'/../../download/'. $_FILES["file-upload"]["name"];
echo "Uploaded ";
$error = "";
// CHECK IF FILE ALREADY EXIST
if (file_exists($destination)) {
$error = $destination . " already exist.";
}
// ALLOWED FILE EXTENSIONS
if ($error == "") {
$allowed = ["application/pdf", "application/x-pdf", "application/acrobat", "applications/vnd.pdf",
"text/pdf". "text/x-pdf"];
$ext = strtolower(pathinfo($_FILES["file-upload"]["name"], PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) {
$error = "$ext file type not allowed. File must be uploaded in PDF format. - " . $_FILES["file-
upload"]["name"];
}
}
// LEGIT TEXT FILE CHECK
if ($error == "") {
if (getimagesize($_FILES["file-upload"]["tmp_name"]) == false) {
$error = $_FILES["file-upload"]["name"] . " is not a valid file.";
}
}
// FILE SIZE CHECK
if ($error == "") {
// 1,000,000 = 1MB
if ($_FILES["file-upload"]["size"] > 50000000) {
$error = $_FILES["file-upload"]["name"] . " - file size too big!";
}
}
// ALL CHECKS OK - MOVE FILE
if ($error == "") {
if (!move_uploaded_file($source, $destination)) {
$error = "Error moving $source to $destination";
}
}
// ERROR OCCURED OR OK?
if ($error == "") {
echo $_FILES["file-upload"]["name"] . " Upload DONE.";
} else {
echo $error;
}
?>
Try change this in HTML:
accept="application/pdf"
It should print the error message, but instead it prints:
"data['clan_id'] != 0){echo "You already have a clan.";} ?>"
here is an image of the error: https://i.imgur.com/BtV7i9s.png
my code:
<html>
<style>
.createclanbutton {
background-color: green;
color: black;
padding: 3px;
border-radius: 9px;
}
</style>
<body>
<?php
if ($user->data['clan_id'] != 0)
{
echo "You already have a clan.";
}
?>
<br><br>
<form action="">
<h3>Create your own clan.<br><br>
Clan Name: <input type="text" name="ClanName" value="Clan Name"><br><br>
Description:<br> <textarea rows="6" cols="50">Brief description of your clan!
</textarea><br>
<button class="createclanbutton">Create your own clan!</button>
</form>
</body>
</html>
I am using phpbb and created a column called "clan_id" in my database which I set at "2" to try and echo the error message.
Looks like you have saved the file as .html or anything? :) You need to save this file as .php!
I have this script from http://lampload.com/component/option,com_jdownloads/Itemid,382/cid,69/task,view.download/
(I am not using a database) I can upload images fine, I can view files, but I want to delete them.
When I press the delete button, nothing happens
http://www.jayg.co.uk/gallery6/upload_gallery.php
<?php
$dir = dirname(__FILENAME__)."/images/gallery" ;
$files1 = scandir($dir);
foreach($files1 as $file){
if(strlen($file) >=3){
$foil = strstr($file, 'jpg'); // As of PHP 5.3.0
$foil = $file;
$pos = strpos($file, 'css');
if ($foil==true){
echo '<input type="checkbox" name="filenames[]" value="'.$foil.'" />';
echo "<img width='130' height='38' src='images/gallery/$file' /><br/>"; // for live host
//echo "<img width='130' height='38' src='/ABOOK/SORTING/gallery-dynamic/images/gallery/ $file' /><br/>";
}
}
}?>
<input type="submit" name="mysubmit2" value="Delete">
</form>
any ideas?
Waw!, the way you are going about it is wrong. For simplicity why don't you implement database
in the codings. Anyway based on your request, I have developed this code along with css file. it has been tested and its working now. Please rate my response and give me a shout if you need futher assistance. .... Sectona
index.php
<?php
extract($_REQUEST);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style>
#box{ width:200px; height:auto; border:solid #999 10px; padding:10px; margin:0 auto; margin-top:100px; background-color:#FFF;-webkit-box-shadow: 0 1px 2px #666;box-shadow: 0 1px 2px #666; color:#66F;}
.link{padding:5px; text-decoration:none; background-color:#333; color:#FFF}
</style>
</head>
<body>
<div id="box">
<img src="images/3.jpg" />
<p></p>
Delete
</div>
</body>
</html>
delete.php
<?php
extract($_REQUEST);
unlink("images/3.jpg");
// Redirect to a page that prints image deleted successfully
header("Location:success_delete.php");
?>
I have exactly same code on my localhost,and it display's everything perfectly,so i copied file to server that im hosting on (000webhost.com) and now it didnt display image,when i open inspect element,it show's that image is there,and image is also uploaded to save folder with that file.Image is at the bottom of the file.
<html>
<head>
<style>
<!--
body{
background-color:black;
}
#form{
position:absolute;
top:180px;
left:37%;
border-radius:20px;
padding:10px;
padding-bottom:50px;
background-color:white;
z-index:1;
}
#login_b{
position:relative;
left:38px;
top:30px;
width:150px;
color:white;
background-color:#777777;
}
#header1 {
position:absolute;
top:115px;
left:39%;
text-shadow:10px 5px 5px #888888;
color:white;
z-index:2;
}
#header2 {
position:absolute;
left:20%;
top:-50px;
font-size:100px;
color: yellow;
}
#image1 {
height:77%;
position:relative;
top:280px;
}
-->
</style>
</head>
</body>
<?php
include 'scripts/config.php';
checkIfLoggedIn();
//error_reporting(0);
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = md5($_POST['password']);
$q = $dbc->prepare("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
$query = $q->execute(array(
$username,
$password
));
$count = $q->fetchColumn();
if($count){
$_SESSION['Username'] = $username;
header('Location: main.php');
return;
} else {
echo '<font color="white">You have entered and incorrect login!</font>';
}
}
?>
<h1 id="header2">ARAM STATS!</h1>
<h1 id="header1">Admin login</h1>
<div id="form">
<form action="" method="POST">
Username:
<input type="text" name="username">
<br>
Password:
<input type="password" name="password">
<br>
<input type="submit" name="submit" value="Login" id="login_b">
</form>
</div>
<img src="/lolimage.jpg" id="image1">
</body>
</html>
Do i have to change something on the server? When i highlight it,it becomes blue: http://piclair.com/album but it doenst display it.Doesnt make any sense.
I don't have enough reputation to comment.
Where is located the image file ? If it's in the same directory of your page you should try
<img src="lolimage.jpg" id="image1">
You mention a "save folder", is it where are located your images ? If so you should add the directory to the path.
<img src="save/lolimage.jpg" id="image1">
Just add your sites base url before your image name.
This may help you
for example,
<img src="<your_sites_base_url>/<image_name>" />
There can be lot of reasons -
try to open the image directly by giving folder path in browser like : xyz.com/yourfolder/imagefolder/images.jpg . if you are able to see it then check the src you have given for image. It should be relative path. use src = "../path/image.jpg" based on location of you html/php file and image. If image is in same folder of the file that no need to add '/'
i am new to php to developing simple content management system i have a field like menu, position, visible, img_des, content, information_id .. it almost all working correctly now i need to insert image field to that . i have a separate file for forms and new page creation
now the following code
if (isset($_POST['submit'])) {
$errors = array();
// clean up data before putting in the database
$information_id = mysql_prep($_GET['info']);
$menu = trim(mysql_prep($_POST['menu'])); // cut out whitespace for menu
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$image = mysql_prep($_POST['image']);
$img_des = mysql_prep($_POST['img_des']);
$content = mysql_prep($_POST['content']);
if(empty($errors)){
$query = "INSERT INTO pages (
menu, position, visible, img_des, content, information_id
) VALUES (
'{$menu}', {$position}, {$visible},'{$img_des}', '{$content}', {$information_id}
)";
if ($result = mysql_query($query, $connection)) {
$message = "The new page was created successfully";
$new_page_id = mysql_insert_id();
redirect_to("content.php?page={$new_page_id}");
} else {
$message = "The page failed to create";
}
?>
before img_des i want to insert image. what type i have i have to use and what code i have to put to insert and retrieve image.
You shouldn't be storing images in the database. Save the image on the server and save a link to the files location in the database.
see this question for more information: What should I use to save a image in database
try this code.
<?php
$images_arr = array();
//This is the directory where images will be saved
$target_dir = "uploads/";
$target = $target_dir.$_FILES['photo']['name'];
//$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];
// Connects to your Database
mysql_connect("Localhost", "remote", "remote123") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
//Tells you if its all ok
echo "The file ". $target_dir.$_FILES['photo']['name']. " has been uploaded, and your information has been added to the directory";
$images_arr[] = $target;
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="http://www.thesoftwareguy.in/favicon.ico" type="image/x-icon" />
<!--iOS/android/handheld specific -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Upload multiple images create thumbnails and save path to database with php and mysql">
<meta name="keywords" content="php, mysql, thumbnail,upload image, check mime type">
<meta name="author" content="Shahrukh Khan">
<title>Upload multiple images create thumbnails and save path to database with php and mysql </title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
.files{height: 30px; margin: 10px 10px 0 0;width: 250px; }
.add{ font-size: 14px; color: #EB028F; border: none; }
.rem a{ font-size: 14px; color: #f00; border: none; }
.submit{width: 110px; height: 30px; background: #6D37B0; color: #fff;text-align: center;}
</style>
<script src="jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function() {
$(".add").click(function() {
$('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div>').appendTo(".contents");
});
$('.contents').on('click', '.rem', function() {
$(this).parent("div").remove();
});
});
</script>
</head>
<body>
<form name="f1" action="index.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="image_form_submit" value="1"/>
<div id="container">
<div id="body">
<div class="mainTitle" >Upload multiple images create thumbnails and save path to database with php and mysql</div>
<div class="height20"></div>
<article>
<div class="height20"></div>
<div style="width: 380px; margin: 0 auto;">
<h3 style="text-align: center;">Image will be resized to 100px X 100px </h3>
<p>Please Enter the Band Members Name.</p>
<p> Band Member or Affiliates Name:</p>
<input type="text" name="nameMember"/>
<p>
Please Enter the Band Members Position. Example:Drums.
</p>
<p>
Band Position:
</p>
<input type="text" name="bandMember"/>
<p>
Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<p>
Please Enter any other information about the band member here.
</p>
<p>
Other Member Information:
</p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
<p>
Please Enter any other Bands the Member has been in.
</p>
<p>
Other Bands:
</p>
<input type="text" name="otherBands" size=30 />
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
</div>
<table class="bordered">
<?php
// fetch all records
$sql = "SELECT * FROM dbProfiles WHERE 1 "; ?>
<?php
$sql="SELECT * FROM dbProfiles";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><p><?php echo $row['nameMember'] ?>"</p></td>
<td><img src="uploads/<?php echo $row['photo'] ?>" width="200px" height="200px;"/><p><?php echo $row['nameMember'] ?>"</p></td>
</tr>
<?php
}
?>
</table>
</article>
</div>
</div>
<div class="height10"></div>
</form>
</body>
</html>
<?php
function errorMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
function successMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #06C;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div>';
}
?>