When uploading images from my website into the designated folder I can see the file in the directory, but cannot open the files nor display them on the webpage.
EDIT This is an issue with my permissions, when trying to open the file in various programs I am receiving permission denied errors.
include('header.php');
$message = "";
$user_id=$_SESSION['user']['user_id'];
$images = getImageCount($user_id);
if(!isset($_SESSION['user']))
{
$_SESSION['message'] = "You must be logged in to manage your images";
header("Location:login.php");
}else if($_SESSION['user']['type'] == INCOMPLETE_USER)
{
$_SESSION['message'] = "You must create a profile to upload images";
header("Location:create_profile.php");
}else if($_SESSION['user']['type'] == DISABLED_CLIENT)
{
$_SESSION['message'] = "Your profile has been disabled";
header("Location:login.php");
}else if($_SERVER['REQUEST_METHOD'] == 'POST')
{
print_r($_FILES);
$user_folder="./profiles/". $user_id;
echo "test";
$file=$_FILES['uploadfile'];
//go to the profile table an SELEECT images FROM profiles WHERE user_id =
if ($images <= MAXIMUM_IMAGES)
{
if ($file['error']!=0)
{
$_SESSION['message']= "Upload Failed!";
}
else if ($_FILES['uploadfile']['type'] != "image/pjpeg" && $_FILES['uploadfile']['type'] != "image/jpeg")
{
$message = "Error! image file must be a'". DEFAULT_FILE_TYPE."'";
}
else if ($file['size'] > MAX_FILE_SIZE)
{
$message = "Error! File must be smaller than '".MAX_FILE_SIZE."' bytes";
}
else
{
$directory = "./profiles/".$user_id;
echo $directory;
//echo $user_folder;
if (!is_dir("profiles/".$user_id))
{
mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
echo 2;
}
$temp_name=$file["tmp_name"];
$new_count = $images + 1;
$file_name=$user_id."_".$new_count;
echo $file_name;
$full_file_name ="profiles/".$user_id."/".$file_name. ".jpg";
move_uploaded_file($temp_name ,$full_file_name);
pg_execute($conn,"update_images",array ($new_count,$_SESSION['user']['user_id']));
}
}
else
{
$message = "Error! no more than " .MAXIMUM_IMAGES . "picture can be uploaded";
}
}
else if (!empty($_POST['submit_changes']))
{
echo "Fail";
$images= $_SESSION['profile']['images'];
}
?>
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $message; ?>
<strong>Select image for upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload New Image" />
<img src="profiles/sault/saultl_4.jpg" alt = "Sault"/>
</form>
<?php
include('footer.php');
?>
I think you did not set the right permission to the folder, just try it in this way:
if (!is_dir("profiles/".$user_id))
{
mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
}
If this do not work we would need some more detailed information in order to help you!
Related
I'm trying to create a form to upload a file, the problem is that the file won't be uploaded. in my code it returns "Image not uploaded".
I've searched a lot online and all the examples uses the same code.
Code:
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("/images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = '/images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype=”multipart/form-data”>
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
The form has also other fields and the data are correctly send to the server.
Try this
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = 'images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype="multipart/form-data">
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
I have already change php.in config where have set max_size = 256M but it still does not allow me to upload. I dont know where i am going wrong.....i am able to upload image file, pdf file ,documentary file but not mp3. php.in settings didnt work for me...please anybody can guide me. Below is my php code
Thanks in advance!
<?php
//Concept of file upload
if(isset($_POST['submit']))
{
$file = $_FILES['files']['name'];
$type = $_FILES['files']['type'];
$file_tmp = $_FILES['files']['tmp_name'];
$size = $_FILES['files']['size'];
$file_err = $_FILES['files']['error'];
if($size!=null)
{
if($_FILES['files']['size'] <= 10000000 && $_FILES['files']['type'] == "audio/mpeg")
{
$path = "D:/";
$path = $path.basename($file);
if(!is_uploaded_file($file))
{
$flag = move_uploaded_file($file_tmp, $path);
if($flag == true)
{
echo "Moved Success";
}
else
{
echo "Some problem";
}
}
else
{
echo "Already Uploaded";
}
}
else
{
echo "Not audio file";
}
}
else if($size > 10000000)
{
echo "Size exceeded";
}
else if($size == null)
{
echo "Please select a file";
}
else
{
echo "Error".$file_err;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="basic.php" method="post" enctype="multipart/form-data">
<input type="file" name="files"/>
<input type="submit" value="upload" name="submit"/>
</form>
</body>
</html>
If you are trying to upload an mp3 file, the type of the $_FILES must be
&& $_FILES["file"]["type"] == "audio/mp3"
not
&& $_FILES['files']['type'] == "audio/mpeg"
I am trying to upload only PDF files in my form, but nothing happens. It doesn't save the file name in the database and the file does not save into the directory.
My PHP code is
if (isset($_POST['submit'])) {
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) {
if ($_FILES['healthfile']['type'] != "application/pdf") {
echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
$filesql = "INSERT INTO tbl_health (link) VALUES ('{$filename}')".die(mysql_error());
$fileresult = mysql_query($filesql, $con).die(mysql_error());
}
if ($fileresult) {
echo 'Success';
} else {
echo 'fail';
}
}
}
my form is
<form action="allhealth.php" method="post" enctype="multipart/form-data">
<label>Upload Your Health Certificate</label>
<span class="btn btn-default btn-file">
Browse <input name="healthfile" type="file">
</span>
<br/><br/>
<button type="button" name="submit" class="btn-success">Submit</button>
</form>
Please help!!
Use this
if(isset($_POST["submit"]))
{
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
$FileType = pathinfo($newname,PATHINFO_EXTENSION);
if($FileType == "pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{
$filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
$fileresult = mysql_query($filesql);
if (isset($fileresult))
{
echo 'File Uploaded';
} else
{
echo 'Something went Wrong';
}
}
else
{
echo "<p>Upload Failed.</p>";
}
}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}
}
Note Before upload file you should check whether its in correct format
and MySQL extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Use this it's working now I have tested at local.
<?php
if (isset($_POST['submit'])) {
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
if ($_FILES['healthfile']['type'] == "application/pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{
$filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
$fileresult = mysql_query($filesql);
}
else
{
echo "<p>Upload Failed.</p>";
}
if (isset($fileresult))
{
echo 'Success';
} else
{
echo 'fail';
}
}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label>Upload Your Health Certificate</label>
<span class="btn btn-default btn-file">
Browse <input name="healthfile" type="file">
</span>
<br/><br/>
<input type="submit" name="submit" class="btn-success" value="submit">
</form>
I'm sure there is a simple solution that I just can't see.
I have a form for uploading stuff.
When the script completes it uses Header('Location: admin.php?success') and a if($_GET['success']) { echo WOOHOO SUCCESS } type message before the rest of the script is run.
The problem with this is that is you want to upload 2 files at once you can't because the first part of the script is executed and nothing else. I then considered using a boolean value to set true or false and display a message from that but that also failed.
I'd like to be able to upload several files in succession and receive a success message for each one.
Many thanks.
relevant PHP:
if(isset($_GET['success']) && empty($_GET['success'])){
echo '<h2>File Upload Successful! Whoop!</h2>';
} else{
if(empty($_POST) === false){
//check that a file has been uploaded
if(isset($_FILES['myTrainingFile']) && !empty($_FILES['myTrainingFile']['tmp_name'])){
file stuff...
if(in_array($fileExt, $blacklist) === true){
$errors[] = "File type not allowed";
}
}
if(empty($errors) === true){
//run update
move file stuff...
}
}
$comments = htmlentities(trim($_POST['comments']));
$category = htmlentities(trim($_POST['category']));
$training->uploadDocument($fileName, $category, $comments);
header('Location: admin.php?success');
exit();
} else if (empty($errors) === false) {
//header('Location: messageUser.php?msg=' .implode($errors));
echo '<p>' . implode('</p><p>', $errors) . '</p>';
}}
}
?>
You need to loop through the $_FILES super-global array and then upload each file.
Here's a working example to give you a better idea.
<?php
$upload_dir= './uploads';
$num_uploads = 2;
$max_file_size = 51200;
$ini_max = str_replace('M', '', ini_get('upload_max_filesize'));
$upload_max = $ini_max * 1024;
$msg = 'Please select files for uploading';
$messages = array();
if(isset($_FILES['userfile']['tmp_name']))
{
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded';
}
elseif($_FILES['userfile']['size'][$i] > $upload_max)
{
$messages[] = "File size exceeds $upload_max php.ini limit";
}
elseif($_FILES['userfile']['size'][$i] > $max_file_size)
{
$messages[] = "File size exceeds $max_file_size limit";
}
else
{
if(#copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i]))
{
$messages[] = $_FILES['userfile']['name'][$i].' uploaded';
}
else
{
$messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
}
}
}
}
?>
<html>
<head>
<title>Multiple File Upload</title>
</head>
<body>
<h3><?php echo $msg; ?></h3>
<p>
<?php
if(sizeof($messages) != 0)
{
foreach($messages as $err)
{
echo $err.'<br />';
}
}
?>
</p>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
<input type="submit" value="Upload" />
</form>
</body>
</html>
Hope this helps!
I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?
<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{
$filename = $_FILES["uploaded_file"]["name"];
$location = "E:\\test_TrainingMaterial/";
$file_size = $_FILES["uploaded_file"]["size"];
$path = $location . basename( $_FILES['uploaded_file']['name']);
if(file_exists($path))
{
echo "File Already Exists.<br/>";
echo "Please Rename and Try Again";
}
else
{
if($file_size < 209715200)
{
$move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
$result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
if ($result)
{
do {
if ($temp_resource = $mysqli->use_result())
{
while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
array_push($rows, $row);
}
$temp_resource->free_result();
}
} while ($mysqli->next_result());
}
if($move)
{
echo "Successfully Uploaded";
}
else
{
echo "File not Moved";
}
}
else
{
echo "File Size Exceeded";
}
}
}
else
{
echo " Invalid File Type";
}
?>
The Html That is used is
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>
Basically you need to add to input name [] brackets and attribute "multiple"
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>
Now all uploaded file will be available via
$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]
and so on
More info at
http://www.php.net/manual/en/features.file-upload.multiple.php