PHP for Absolute Beginners PHẦN 7

Phân biệt giữa các trang đa-nhập cảnh và nhập cảnh đơn Trong chương kế tiếp, bạn sẽ học làm thế nào để cập nhật các mục đã được tạo ra , mang lại cho bạn một bước gần hơn để có một ứng dụng viết blog hoàn toàn tùy hiện các dữ liệu cơ bản làm vệ sinh $ e = sanitizeData ($ e) | CHAPTER 8 ADDING THE ABILITY TO UPLOAD IMAGES In your image handling snippet you place your object instantiation and processing within a try block. Doing so ensures that you can output the custom error message by grabbing the message within the catch block if any of the custom errors you define within the class are thrown. Inside your try block you instantiate the ImageHandler object and pass simple_blog as its argument which sets the save_dir property. Next you call processUploadedImage and pass the uploaded file as its argument. processUploadedImage returns the file s path so you store that in a variable called img_path which you use for now to output the image for viewing via an img HTML tag. Finally you output the image path as plain text and exit the script to prevent your test entries from being saved to the database. You can test your class by uploading an image. Navigate to your admin form in a browser and fill out the form with test data you don t save this then select a file to upload. When you press the Save Entry button you should see your image displayed along with its path see Figure 8-2 . Figure 8-2. An image uploaded by your ImageHandler class If you look at your simple_blog folder in the file system you ll see that the image has been saved see Figure 8-3 . Download at 227 CHAPTER 8 ADDING THE ABILITY TO UPLOAD IMAGES Figure 8-3. The uploaded image saved in the simple_blogfolder This isn t necessarily bad but it does cause clutter in your folder. You can clear this up by creating a new folder to store images in. Creating a New Folder You could simply create the folder manually but it s better to use PHP to check whether a folder exists then create it if it doesn t. This way you have to change only the path if the folder you wish to save images in changes in the future the alternative is to go in and manipulate the file system directly. You can make a new folder by creating a new method in your ImageHandler class called checkSaveDir that

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.