473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP txt/doc Upload, and make url to it?

117 100+
Ok, i want to make a simple upload where it accepts only txt/doc files rejects other files and saves (tru) files to a folder uploads
.
After display a message (upload complete) Link:
And have it post the url to the txt/doc file they just uploaded.

so it will pop up like uploads/myfile.txt, so they can link it in the walkthrough form.
So far i have 2 files, addwalkthrough.php and savewalkthrough.php but i cant get it to echo the file link...

[PHP]<form action="savewalkthrough.php" 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>

<?php
if (($_FILES["file"]["type"] == ".txt")
|| ($_FILES["file"]["type"] == ".doc")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>[/PHP]

What did i do wrong it keeps saying invalid file...
.txt
Sep 4 '07 #1
8 5987
gregerly
192 Expert 100+
Hi there Breana,

I think your error is coming in the way you are using the $_FILES['type'] variable from the $_FILES superglobal. Type is actually the type of file you are uploading, not the file extention. If you wanted to check based on the file extention, you could do something like this:

[PHP]<?
$fileType=explode('.',$_FILES['type']);
$type=$fileType[1];

//will give you the actual file extention
echo $type;
?>[/PHP]

If you want to see the different elements you have available to you, you could run the code below:

[PHP]<?php
foreach($_FILES as $k => $v){
foreach ($_FILES[$k] as $kk => $vv){
echo "$kk => $vv<br />";
}
}
?>[/PHP]

So, your code is working properly. Congrats! It may not be doing what you want, but you should be able to tweak the "type" to get the desired result. If you have any other problems post back here.

In looking for more information, I noticed that w3schools has almost the exact same PHP code that your using. Note the way they are using the "type", it's not a file extention, rather something like "image/gif" or text/plain.

Greg
Sep 5 '07 #2
Breana
117 100+
Lol, i am using a php book my dad baught me :)
But it dont give fils ext. or how to set them that i see anyway lol.
I dont get this:

[PHP]$fileType=explode('.',$_FILES['type']);
$type=$fileType[1];
//will give you the actual file extention
echo $type;[/PHP]

I need it to check if its a .txt, .doc and upload it.
What does that do?
I just want to know how to get it to detext the wright files in the code above lol.

Is it .text?
Sep 5 '07 #3
Atli
5,058 Expert 4TB
Hi.

The $_FILES[$x]['type'] variable stores the MIME type of the uploaded file.
For text files this would return 'text/plain' and for .doc types it should return 'application/msword'.

Check out this list of MIME types at w3schools.com
Sep 5 '07 #4
gregerly
192 Expert 100+
Lol, i am using a php book my dad baught me :)
But it dont give fils ext. or how to set them that i see anyway lol.
I dont get this:

[PHP]$fileType=explode('.',$_FILES['type']);
$type=$fileType[1];
//will give you the actual file extention
echo $type;[/PHP]

I need it to check if its a .txt, .doc and upload it.
What does that do?
I know this stuff can be confusing. In your original code your saying

if $_FILES["file"]["type"] == ".txt" then do something. This is incorrect as your checking the file extention, and not the file type. The explode code I mentioned before would allow you to check the file extention. To be more accurate, your upload code should check like this:

[PHP]<?
if($_FILES["file"]["type"] == "application/msword" || $_FILES["file"]["type"] =="text/plain"){
//do something
}
?>[/PHP]

You just need to change how your using the files array, and check for the proper type, instead of checking for file extention.

Post back if you need more help. This can be a very confusing topic, and we are here to help!

Greg
Sep 5 '07 #5
Breana
117 100+
Ok, i see now i used to do it like .txt in html lol.
It works but no file was uploaded?

Upload: testgame.txt
Type: text/plain
Size: 0.0625 Kb
Temp file: /tmp/phpKacwZt
Stored in: uploads/testgame.txt
Time: 1.1 seconds
Sep 5 '07 #6
gregerly
192 Expert 100+
Ok, i see now i used to do it like .txt in html lol.
It works but no file was uploaded?

Upload: testgame.txt
Type: text/plain
Size: 0.0625 Kb
Temp file: /tmp/phpKacwZt
Stored in: uploads/testgame.txt
Time: 1.1 seconds
gotta love that feeling when something works!

Congrats, post back here if you need assistance on anything else!

Greg
Sep 5 '07 #7
Breana
117 100+
Uh.. yeah it wprked but no files was put into the uploads/ folder?
Sep 5 '07 #8
Breana
117 100+
Never mind, it crashes at the tmp/ lol i forgot to ad that folder duh.....

UPDATED :)

Upload: testgame.txt
Type: text/plain
Size: 0.0625 Kb
Temp file: /tmp/php4a9ydf
testgame.txt already exists.

Sorry, you already uploaded that file...
Sep 5 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stephane | last post by:
Hello, I need to allow an end user to upload video files on a server and in the same time to put the file name and a few infos in a database. It must be very simple for the end user, and...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
4
by: NohaKhalifa | last post by:
Dear All; I'm developing a web site and i need to make adminisration for this site it's a site for Real Estates . But I don't need the administration to be online .. I want them to fill data...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
3
by: Mike Kelly | last post by:
Hi. I've built a page using standard ASP.NET 2.0 features and when I upload a large file (>20MB) to our intranet server, I get a paltry 100KB/s on our 100Mb/s LAN. Simply copying the file, I get...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
9
by: Steve Poe | last post by:
I work for an animal hospital trying to use PHP to store an animal's dental x-rays to a file server. I can browse for the xray on the local desktop computer then click "Upload Image". This...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
12
by: GuangXiN | last post by:
I want the file upload element disappear, instead of it, I place a text box and a button with my own css defination. but it doesn't work on IE7. What should I do now? <form action="upload.php"...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.