Uploading Filetypes and placing them in seperate folders.
Posted on
16th Feb 2014 07:03 pm by
admin
Hello,
first post , and asking for help im afraid. Very new to PHP, was making good progress I thought , but im stuck with this part. I have a uploading form that works perfectly and uploads everything to "uploads/images" . What Im trying to do is upload .txt or .rtf files to "uploads/text" and .jpg and .gif's to "uploads/images . I thought this wasnt going to be too difficult but im totally stuck and have spent hours trying to solve this. If anyone could help at all and show my what I'm doing wrong, Id be very very grateful!
This is the code as it stands. Right now, it ALWAYS goes to "uploadfail.php" and the file does not upload. If I remove all the code relating to filetype then both the text and image files succesully upload and I go to "uploadsucess.php"
Code: <?
require('connection.php');
//my_sql_real..... makes it safe against mysql injection
$title = mysql_real_escape_string($_POST['title']);
$date = mysql_real_escape_string($_POST['date']);
$author = mysql_real_escape_string($_POST['author']);
$description = mysql_real_escape_string($_POST['description']);
$type = mysql_real_escape_string($_POST['type']);
$imgName = $_FILES['pic']['name'];
$imgTmp = $_FILES['pic']['tmp_name'];
$imgSize = $_FILES['pic']['size'];
$imgType = $_FILES['pic']['type'];
$maxFileSize = 200000;
if ($imgType == "image/jpeg" || $imgType == "image/gif" || $imgType == "application/rtf" || $imgType == "application/x-rtf" || $imgType == "text/richtext" || $imgType == "text/plain" ) {
$error = "";
} else {
$error = "type";
}
if ($imgType == "image/jpeg" || $imgType == "image/gif" ) {
$filetype = "image";
}
if ($imgType == "application/rtf" || $imgType == "application/x-rtf" || $imgType == "text/richtext" || $imgType == "text/plain" ) {
$filetype = "text";
} else {
$filetype = "other";
}
if ($imgSize > $maxFileSize) {
$error = "size";
}
if ($error == "" && $imgName != "" && $filetype == "image" ) {
$filePath = "uploads/images/".$imgName;
move_uploaded_file($imgTmp, $filePath);
if ($error == "" && $imgName != "" && $filetype == "text" ) {
$filePath = "uploads/text/".$imgName;
move_uploaded_file($imgTmp, $filePath);
//another securtiy measure sprintf
$query = sprintf("INSERT INTO gallery(title,date,author,type,description,path) VALUES ('%s' , '%s' ,'%s' ,'%s' ,'%s' ,'%s' )", $title,$date,$author,$type,$description,$filePath);
if (!mysql_query($query, $connect)) {
die();
} else {
header("Location: uploadsuccess.php");
}
}
} else {
header("Location: uploadfail.php");
}
?>
No comments posted yet
Your Answer:
Login to answer
271 42
Other forums
Buggy registration system
Hey, I just started scripting in PHP, and I ran into a few problems.
Code: <?php
includ
IMAGE - SERVER SIDE scripting help...
How to receive pixel data of an image and buid a bitmap file on the server using the HTTP POST metho
Sessions work for me and not others?
I'm having a bit of bad luck with sessions. In the past they have worked fine for me, but this time
mysql_real_escape_string making variable equal nothing
i post a form and i post the variable:
Code: $var = $_POST[variable];then i echo $var its what i
How to know if online site made with PHP
Hello,
I see some sites that does not display extensions at all , for example:
www.site.co
Output Full URL of Current Page
Let's say I am currently viewing a dynamic page:
http://www.mydomain.com/directory/index.php
Looking for help reading a .txt or .ini file and outputting the info.
I have a file called Info.ini and It has the following info:
Code: [General]
Online=0
I wou
Securing a user input - need some confirmation
Hello All,
I am in the process of recoding a large proportion of an e-commerce site, one of t
Sql and php order list script problems
Hi im having a few problems with trying to get the coding right on a orders list ive done (or trying
Warehouse Management
Hi,
On our system, we have two storage locations ( A and B ) where A is the main factory