473,396 Members | 2,018 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,396 software developers and data experts.

I am trying to assign a unique file name to an uploaded file keeping the extension

I am new to PHP and I am trying to get this code to rename a file and save it in the subdirectory. But I need it to keep the extension. Please Help me!

<?php
$allowedExts = array("txt", "dxf","DXF", "pdf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo ".tmpFiles: " . $_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(".tmpFiles/" . $_FILES["file"]["name"]))
{

echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
".tmpFiles/" . $_FILES["file"]["name"]);
echo "Stored in: " . ".tmpFiles/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}

include('list.php');

?>
Thanks in advance for you help
Nov 17 '13 #1
2 1486
johny10151981
1,059 1GB
Describe the error please.
Nov 19 '13 #2
I am not getting an error I just do not know how to do it. I have tried uniqid() but I am not sure where to put it and what syntax to use
Nov 19 '13 #3

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

Similar topics

3
by: Rick | last post by:
Does anyone have a bit of sample code to use with Access 2000 in order to use the Windows API to change an external file name or file name extension? For example, if I export a query to a text...
2
by: rxfrenc | last post by:
Hello- I have a problem trying to transfer text to a file. I need to include periods (.) in the file name, but it keeps changing them to pound signs (#). Here is what I have in the 'File Name'...
33
by: jacob navia | last post by:
Recently there was a discussion in this group about how to retrieve the file name given a FILE *. The question raised my curiosity, and after some research I have come up with a good...
0
by: John Clark | last post by:
Hi, I am using webbrowser control to automate a site. this site contains form with tage <INPUT TYPE=FILE ...> I know the control is not writeable. So, only thing that I could do was: ...
13
by: poddar | last post by:
Hi, I am new to this group. I had just one small question Is it possible to get the file name back from file descriptor. i.e. I have written a function to overload the libc write using...
3
by: sheeeng | last post by:
Hi all, I know that we can check for invalid file name characters as shown at http://www.codeproject.com/useritems/reallyusefulpath.asp. But how can we check a string for invalid file name...
2
by: dobbine | last post by:
hi there folks, I was wondering if I have a folder with the following files in it, can I return 00012a.jpg? 00012.jpg 00012a.jpg 00012b.jpg 00012c.jpg right up to ... 00012z.jpg
10
by: Academia | last post by:
I'd like to check a string to see that it is a valid file name. Is there a Like pattern or RegEx that can do that. 1) Just the file name with maybe an extension 2)A full path An help...
0
by: vasikaran | last post by:
Hi , i hava one ftp script in batch file programming , ftp script is working fine.. but my is i dont know how much files and what are files avaiulable in remote folder ,,,, if any errors or...
2
by: ragonz | last post by:
im having trouble in executing sql loader command in php. i already posted this issue in php forum, but didn't give me clear answer yet. probably posting in this forum would be better. here's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.