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");
}
?>

271 Like 42 Dislike
Previous php-forum Next php-forum
Other php-forum

Reading waves
Hi,

I am trying to find a way of finding the highs and lows on a graph line, the line points

check if value exists
I have googled this for a while and I am getting lots of different results. Is there a standard meth

Am i doing this wrong?
Im pulling from an Oracle database some info... one of which is a field count_pnums which is a NUMBE

How to secure my login data to my database?
Hi there,

It may be a stupid question but I don't know how to secure my login data to my data

Running External Scripts
I am very new to PHP and am not even sure what I'm asking is possible. I have just installed mediaW

CRM?
I think that's what they're called. Customer Management System? Anyway. Is there any good free ones

Email/MMS to Site
Hey guys. I'm looking for a way for a user to upload images and/or text via their cell phones. It

PHP mail() rejected by SMTP
I'm currently working on a newsletter application which is installed on a windows 2003 server runnin

Getting the full city list from maxminds geoip database
I am suprised I can't find any references to this.

I have maxminds geoip lite installed and i

db entry based on primary key
My "topics" table contains 10 entires
*--------------*
topicid topic
------

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash