I am trying to upload a file and then rename the file as something else.
Everything worked fine unless the renamed filename already existed. So I added
some extra code to check if the filename already existed and if so to delete it
before renaming the uploaded file. Everything now works fine in the following
code:
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
echo '<pre>';
if (move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$imagefilename = $_POST['imagefilename'];
$renamedfile = $uploaddir . $imagefilename;
if (file_exists($r enamedfile)) {
unlink($renamed file);
}
rename($uploadf ile , $renamedfile);
}
else {
echo "There was an error in your file upload. \n";
echo 'Here is some more debugging info:';
print_r($_FILES );
}
That seems like a lot of code to do something relatively simple. So my question
is, is there a simpler way like forcing the rename function to overwrite an
existing file with the same filename if it exists.
Regards
Dynamo 4 2194
Dynamo wrote: I am trying to upload a file and then rename the file as something else. Everything worked fine unless the renamed filename already existed.
Are you sure? The documentation for move_uploaded_f ile says: If the destination file already exists, it will be overwritten.
You can do an unlink and then move the file, even if the destination
does not exist:
@unlink($destin ation);
move_uploaded_f ile($source, $destination);
The @ is there to avoid any errors if the $destination file does not
exist.
Yes I'm sure. It is not move_uploaded_f ile that is causing the problem. It is
the rename function that is causing the headache. To explain better, lets say
I'm uploading a picture called mypicture.jpg. There is nothing in the uploaded
filename that associates it with a record in a mysql table. So once the file has
been uploaded I then rename it as 21.jpg by using
rename('mypictu re.jpg','21.jpg ')
I can now associate 21.jpg with a record in my table where the id=21
The problem lies in the fact that at some point in time I might want to change
the picture that is asscociated with this record. In that instance if I use
rename('mynewpi cture.jpg','21. jpg')
then 21.jpg already exists and I get a warning message that the file already
exists and hence is not renamed.
Hope that explains it more fully.
Regards
Dynamo
In article <11************ **********@u72g 2000cwu.googleg roups.com>, Sjoerd
says... Are you sure? The documentation for move_uploaded_f ile says: If the destination file already exists, it will be overwritten.
Dynamo wrote: Yes I'm sure. It is not move_uploaded_f ile that is causing the problem. It is the rename function that is causing the headache. To explain better, lets say I'm uploading a picture called mypicture.jpg. There is nothing in the uploaded filename that associates it with a record in a mysql table. So once the file has been uploaded I then rename it as 21.jpg by using rename('mypictu re.jpg','21.jpg ')
Why store it with the actual filename and rename it afterwards? You'll
have 2 files, one of which you aren't going to use... Or are you doing
something else with it?
$uploaddir = 'images/';
if (!move_uploaded _file($_FILES['userfile']['tmp_name'], $uploaddir.
$_POST['imagefilename'])) {
echo "There was an error in your file upload. \n";
echo 'Here is some more debugging info:';
print_r($_FILES );
}
Dynamo wrote: That seems like a lot of code to do something relatively simple. So my question is, is there a simpler way like forcing the rename function to overwrite an existing file with the same filename if it exists.
Yes, file upload can be a PITA, if not done right or well.
After searching, digging through scripts, I finally sat down and made a
class for myself to handle all this.
A few friends tol me to shar eit, so I did... https://sourceforge.net/projects/php-yacs
the DOWNLOAD has nothing behind it yet, as I can't figure out how to do
that yet. But you go to CVS link, you can get the FILE directory (which
contains the php files) from there.
Hope it helps you.
It renames, moves, copies files as you decide. defines a new name for
an upload if you like. Will overwrite files, if you like, etc.
contact tme if you have any questions (or can tell me how to get SF to
have my set of files download via thier DOWNLAOD button.
Walter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bob Bedford |
last post by:
I've tried everything seen on Internet, but nothing works:
I'm trying to move a file from a dir to a subdir (WinXP Pro, Apache, PHP
4.3.3)
Here is the code:
chmod($XMLPath, 0777);
chmod($XMLPath.$file, 0777);
chmod($XMLPath.$successpath, 0777);
|
by: mcp6453 |
last post by:
I am trying to use Jack's FormMail script
(http://www.dtheatre.com/scripts/formmail). Since I'm brand new at PHP
and not very good at HTML, I have an easy question, which I will narrow
down. When the email arrives, it has this information:
v_firstname: asdf
v_lastname: asdf
b_email: asdf@bellsouth.net
v_phone: asdf
v_cellphone: asdf
|
by: rbt |
last post by:
Can someone detail the differences between these two? On Windows which
is preferred?
Also, is it true that win32api.DeleteFile() can remove the 'special'
files located in the 'special' folders only accessible by the shell
object such as Temporary Internet Files, etc.
Thanks!
|
by: Dejan |
last post by:
Hy,
Sorry for my terreble english
I have this simple code for deleting rows in mysql table...
Everything works fine with it.
So, what do i wanna do...:
my sql table looks something like this:
id Name Surname pictr0 picrt1 pictr2
pictr3
|
by: mantrid |
last post by:
Im trying to move a file but am having luck
my code is below. The temp and target paths are valid as they echo
correctly. but I cant get the copy() function to work, or the rename()
function
$target_path = $targetdir . basename($uploadedfile);
$temp_path = $tempdir. basename($uploadedfile);
echo "Target - ".$target_path." Temp - ".$temp_path."<br>";
| |
by: gsoguerrilla |
last post by:
Hi,
I have limited knowledge in php and I am having trouble with uploading
an image to a remote directory and resizing it if it's larger and
renaming it to a unique id, while at the same time I would like to
create a record in mysql database. I've tried to find some tutorials
but I've had trouble finding any good ones. Wondering if anybody has
good examples or link to a good tutorial.
Thanks in advance.
|
by: nt91rx78 |
last post by:
Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problw\em.
This is program C problem
Anyone can help me with this problem, please!!!!!!!!!
This is the problem:
3. Several input text files have been provided as input to your program.
a) Write a function to combine these files into a single file.
b) Write a function to take care of...
|
by: Milagro |
last post by:
Hello Everyone,
I'm trying to debug someone elses php code. I'm actually a Perl
programmer, with OO experience, but not in php.
The code is supposed to upload a photo from a form and save it both on
the file system and in a mySql database. 90% of the time it works just
fine. But for some reason, with some photos, it doesn't work. The
image never shows up on the filesystem and there is no entry in the
database. I feel it's a problem...
|
by: daithimcc |
last post by:
I have been trying to delete a file using a script and it is refusing to go. (I have very little experience of Perl). One script creates the temporary file (empty) and later another is to delete it.
The url passes a parameter to the script: e.g.
dfile.pl?f=abcde
The filename f obviously varies.
In the script:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |